gpt4 book ai didi

c# - 将log4net文件推送到ElasticSearch

转载 作者:行者123 更新时间:2023-12-02 22:57:07 28 4
gpt4 key购买 nike

我已经编写了C#WebAPI,并希望使用log4net将请求记录到文件中。将日志写入日志文件后,我想将这些日志文件(将使用DelegateHandler捕获所有请求)推送到ElasticSearch。我到处搜索,但是找不到解决问题的方法。以下是我拥有的log4net.config文件(ElasticAppender不起作用)

   <log4net>
<root>
<appender-ref ref="FileAppender" />
<appender-ref ref="ElasticSearchAppender" />
</root>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Logs/API_.log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyy-MM-dd" />
<maximumFileSize value="10MB" />
<preserveLogFileNameExtension value="true"/>
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss.fff}, %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="ERROR" />
</filter>
</appender>
<appender name="ElasticSearchAppender" type="log4net.ElasticSearch.ElasticSearchAppender, log4stash">
<Server>elasticnode01</Server>
<Port>9200</Port>
<IndexName>apilogentry</IndexName>
<ElasticFilters>
<Filter type="log4net.ElasticSearch.Filters.RenameKeyFilter, log4stash">
<Key>processId</Key>
<RenameTo>ServiceName</RenameTo>
</Filter>
<Filter type="log4net.ElasticSearch.Filters.RenameKeyFilter, log4stash">
<Key>Message</Key>
<RenameTo>message</RenameTo>
</Filter>
<Grok>
<SourceKey>message</SourceKey>
<Pattern>%{NUMBER:ResponseCode} %{WORD:Method} %{URIPATHPARAM:Url} %{NUMBER:ElapsedMls} %{GREEDYDATA:StatusMessage}</Pattern>
</Grok>
</ElasticFilters>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="ERROR" />
</filter>
</appender>
</log4net>

这是我用来收集数据的类(我想记录日志)。
  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.
}

此类处理所有HTTP请求,并应记录信息。现在信息正在记录到文件中。
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;
}
}

如何将这些文件的输出获取到ElasticSearch?我已经阅读了有关FileBeat和Logstash的信息,但不确定如何使它们全部运行。有人对我可能会缺少的东西有任何想法吗? (我知道log4net.config中的grok条目不是正确的(我试图映射到ApiLogEntry类,但不知道如何进行)。

TIA

最佳答案

请遵循以下文档
Log4net.ElasticSearch NuGet Lib,您应该能够实现自己的目标。

关于c# - 将log4net文件推送到ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49521659/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com