C# 将log4net文件推送到ElasticSearch
我已经编写了c#WebAPI,希望使用log4net将请求记录到文件中。日志写入日志文件后,我想将这些日志文件(将使用DelegateHandler捕获所有请求)推送到ElasticSearch。我到处找了,但找不到解决问题的办法。以下是我拥有的log4net.config文件(ElasticAppender不工作) 这是一个处理所有HTTP请求并记录信息的类。现在,这些信息正在被记录到文件中C# 将log4net文件推送到ElasticSearch,c#,
elasticsearch,log4net,C#,
elasticsearch,Log4net,我已经编写了c#WebAPI,希望使用log4net将请求记录到文件中。日志写入日志文件后,我想将这些日志文件(将使用DelegateHandler捕获所有请求)推送到ElasticSearch。我到处找了,但找不到解决问题的办法。以下是我拥有的log4net.config文件(ElasticAppender不工作) 这是一个处理所有HTTP请求并记录信息的类。现在,这些信息正在被记录到文件中 public class ApiLogHandler : DelegatingHandler {
public class ApiLogHandler : DelegatingHandler
{
ILogger _logger;
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var apiLogEntry = CreateApiLogEntryWithRequestData(request);
if (request.Content != null)
{
await request.Content.ReadAsStringAsync()
.ContinueWith(task =>
{
apiLogEntry.RequestContentBody = task.Result;
}, cancellationToken);
}
return await base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
var response = task.Result;
// Update the API log entry with response info
apiLogEntry.ResponseStatusCode = (int)response.StatusCode;
apiLogEntry.ResponseTimestamp = DateTime.Now;
if (response.Content != null)
{
// apiLogEntry.ResponseContentBody = response.Content.ReadAsStringAsync().Result;
apiLogEntry.ResponseContentType = response.Content.Headers.ContentType.MediaType;
apiLogEntry.ResponseHeaders = SerializeHeaders(response.Content.Headers);
}
// TODO: Save the API log entry to the database
_logger.Info(apiLogEntry);
return response;
}, cancellationToken);
}
private ApiLogEntry CreateApiLogEntryWithRequestData(HttpRequestMessage request)
{
var context = ((HttpContextBase)request.Properties["MS_HttpContext"]);
var routeData = request.GetRouteData();
return new ApiLogEntry
{
Application = "Search.API",
User = context.User.Identity.Name,
Machine = Environment.MachineName,
RequestContentType = context.Request.ContentType,
RequestRouteTemplate = routeData.Route.RouteTemplate,
//RequestRouteData = SerializeRouteData(routeData),
RequestIpAddress = context.Request.UserHostAddress,
RequestMethod = request.Method.Method,
RequestHeaders = SerializeHeaders(request.Headers),
RequestTimestamp = DateTime.Now,
RequestUri = request.RequestUri.ToString()
};
}
private string SerializeRouteData(IHttpRouteData routeData)
{
return JsonConvert.SerializeObject(routeData, Formatting.None, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
//return JsonConvert.SerializeObject(routeData, Formatting.Indented, new JsonSerializerSettings
//{
// PreserveReferencesHandling = PreserveReferencesHandling.Objects
//});
}
private string SerializeHeaders(HttpHeaders headers)
{
var dict = new Dictionary<string, string>();
foreach (var item in headers.ToList())
{
if (item.Value != null)
{
var header = String.Empty;
foreach (var value in item.Value)
{
header += value + " ";
}
// Trim the trailing space and add item to the dictionary
header = header.TrimEnd(" ".ToCharArray());
dict.Add(item.Key, header);
}
}
return JsonConvert.SerializeObject(dict, Formatting.Indented);
}
public ApiLogHandler(ILogger logger)
{
_logger = logger;
}
}
公共类ApiLogHandler:DelegatingHandler
{
ILogger\u记录器;
受保护的覆盖异步任务SendAsync(HttpRequestMessage请求,CancellationToken CancellationToken)
{
var apiLogEntry=CreateApiLogEntryWithRequestData(请求);
if(request.Content!=null)
{
wait request.Content.ReadAsStringAsync()
.ContinueWith(任务=>
{
apiLogEntry.RequestContentBody=task.Result;
},取消令牌);
}
return wait base.sendaync(请求、取消令牌)
.ContinueWith(任务=>
{
var response=task.Result;
//使用响应信息更新API日志条目
apiLogEntry.ResponseStatusCode=(int)response.StatusCode;
apiLogEntry.ResponseTimestamp=DateTime.Now;
if(response.Content!=null)
{
//apiLogEntry.ResponseContentBody=response.Content.ReadAsStringAsync().Result;
apiLogEntry.ResponseContentType=response.Content.Headers.ContentType.MediaType;
apiLogEntry.ResponseHeaders=SerializeHeaders(response.Content.Headers);
}
//TODO:将API日志条目保存到数据库
_logger.Info(apiLogEntry);
返回响应;
},取消令牌);
}
私有ApiLogEntry CreateApiLogEntryWithRequestData(HttpRequestMessage请求)
{
var context=((HttpContextBase)request.Properties[“MS_HttpContext”]);
var routeData=request.GetRouteData();
返回新的ApiLogEntry
{
Application=“Search.API”,
User=context.User.Identity.Name,
Machine=Environment.MachineName,
RequestContentType=context.Request.ContentType,
RequestRouteTemplate=routeData.Route.RouteTemplate,
//RequestRoutedData=序列化RoutedData(RoutedData),
RequestIpAddress=context.Request.UserHostAddress,
RequestMethod=request.Method.Method,
RequestHeaders=SerializeHeaders(request.Headers),
RequestTimestamp=DateTime。现在,
RequestUri=request.RequestUri.ToString()
};
}
私有字符串序列化路由数据(IHttpRouteData路由数据)
{
返回JsonConvert.SerializeObject(RoutedData,Formatting.None,new JsonSerializerSettings())
{
ReferenceLoopHandling=ReferenceLoopHandling.Ignore
});
//返回JsonConvert.SerializeObject(路由数据、格式、缩进、新JsonSerializerSettings
//{
//PreserveReferencesHandling=PreserveReferencesHandling.Objects
//});
}
私有字符串序列化头(HttpHeaders头)
{
var dict=新字典();
foreach(headers.ToList()中的var项)
{
如果(item.Value!=null)
{
var header=String.Empty;
foreach(item.value中的var值)
{
标题+=值+“”;
}
//修剪尾随空格并将项添加到字典中
header=header.TrimEnd(“.ToCharArray());
dict.Add(item.Key,header);
}
}
返回JsonConvert.Serialized对象(dict,Formatting.Indented);
}
公共ApiLogHandler(ILogger记录器)
{
_记录器=记录器;
}
}
如何将这些文件的输出获取到ElasticSearch?我读过关于FileBeat和logstash的文章,但不知道如何让这一切运行起来。有人知道我可能遗漏了什么吗?(我知道log4net.config中的grok条目不正确(我正在尝试映射到ApiLogEntry类,但不知道如何继续)
TIA遵循
您应该能够实现您的目标。弹性搜索Log4Net appender的文档
它建议使用推送日志文件数据(用于数据转换/解析)进入弹性搜索数据库。在编写本文时,所有这些组件都可以在上自由使用。这看起来不像是答案。这应该在comment@TarangP我没有50个名声来对原始问题添加评论。我添加这个作为一个答案,因为它可能会帮助某人。
public class ApiLogEntry
{
public long ApiLogEntryId { get; set; } // The (database) ID for the API log entry.
public string Application { get; set; } // The application that made the request.
public string User { get; set; } // The user that made the request.
public string Machine { get; set; } // The machine that made the request.
public string RequestIpAddress { get; set; } // The IP address that made the request.
public string RequestContentType { get; set; } // The request content type.
public string RequestContentBody { get; set; } // The request content body.
public string RequestUri { get; set; } // The request URI.
public string RequestMethod { get; set; } // The request method (GET, POST, etc).
public string RequestRouteTemplate { get; set; } // The request route template.
public string RequestRouteData { get; set; } // The request route data.
public string RequestHeaders { get; set; } // The request headers.
public DateTime? RequestTimestamp { get; set; } // The request timestamp.
public string ResponseContentType { get; set; } // The response content type.
public string ResponseContentBody { get; set; } // The response content body.
public int? ResponseStatusCode { get; set; } // The response status code.
public string ResponseHeaders { get; set; } // The response headers.
public DateTime? ResponseTimestamp { get; set; } // The response timestamp.
}
public class ApiLogHandler : DelegatingHandler
{
ILogger _logger;
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var apiLogEntry = CreateApiLogEntryWithRequestData(request);
if (request.Content != null)
{
await request.Content.ReadAsStringAsync()
.ContinueWith(task =>
{
apiLogEntry.RequestContentBody = task.Result;
}, cancellationToken);
}
return await base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
var response = task.Result;
// Update the API log entry with response info
apiLogEntry.ResponseStatusCode = (int)response.StatusCode;
apiLogEntry.ResponseTimestamp = DateTime.Now;
if (response.Content != null)
{
// apiLogEntry.ResponseContentBody = response.Content.ReadAsStringAsync().Result;
apiLogEntry.ResponseContentType = response.Content.Headers.ContentType.MediaType;
apiLogEntry.ResponseHeaders = SerializeHeaders(response.Content.Headers);
}
// TODO: Save the API log entry to the database
_logger.Info(apiLogEntry);
return response;
}, cancellationToken);
}
private ApiLogEntry CreateApiLogEntryWithRequestData(HttpRequestMessage request)
{
var context = ((HttpContextBase)request.Properties["MS_HttpContext"]);
var routeData = request.GetRouteData();
return new ApiLogEntry
{
Application = "Search.API",
User = context.User.Identity.Name,
Machine = Environment.MachineName,
RequestContentType = context.Request.ContentType,
RequestRouteTemplate = routeData.Route.RouteTemplate,
//RequestRouteData = SerializeRouteData(routeData),
RequestIpAddress = context.Request.UserHostAddress,
RequestMethod = request.Method.Method,
RequestHeaders = SerializeHeaders(request.Headers),
RequestTimestamp = DateTime.Now,
RequestUri = request.RequestUri.ToString()
};
}
private string SerializeRouteData(IHttpRouteData routeData)
{
return JsonConvert.SerializeObject(routeData, Formatting.None, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
//return JsonConvert.SerializeObject(routeData, Formatting.Indented, new JsonSerializerSettings
//{
// PreserveReferencesHandling = PreserveReferencesHandling.Objects
//});
}
private string SerializeHeaders(HttpHeaders headers)
{
var dict = new Dictionary<string, string>();
foreach (var item in headers.ToList())
{
if (item.Value != null)
{
var header = String.Empty;
foreach (var value in item.Value)
{
header += value + " ";
}
// Trim the trailing space and add item to the dictionary
header = header.TrimEnd(" ".ToCharArray());
dict.Add(item.Key, header);
}
}
return JsonConvert.SerializeObject(dict, Formatting.Indented);
}
public ApiLogHandler(ILogger logger)
{
_logger = logger;
}
}