c# - 如何使用 Serilog 和 ElasticSearch 拥有不同的日志类型
问题描述
我目前正在尝试更改我们的系统配置以使用Serilog (而不是使用 FileBeat 作为 LogStash 的托运人)
我们还在各种查询中使用日志类型字段(在 FileBeat 配置文件中很容易配置)并在 Elastic 上索引日志。
问题是,当使用 Serilog 时,我们得到默认类型logevent,但我没有找到可以配置它的位置。我想有一个选项来确定每个 Serilog 实例的特定日志类型。目前,我的所有日志都有默认类型。
我的 Serilop 配置是:
var path = GetLogPath();
var logger = new LoggerConfiguration()
.MinimumLevel.Information()
.Enrich.WithMachineName()
.Enrich.WithProperty("RequestId", Guid.NewGuid())
.WriteTo.RollingFile(
pathFormat: path,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u4}] [{RequestId}] {Message}{NewLine}{Exception}", buffered: false, shared: true);
logger.WriteTo.Elasticsearch(
new ElasticsearchSinkOptions(new Uri(this.configurationService.ElasticSearchUrl())));
如何更改日志类型?
编辑
经过一番调查,我发现我想更改LoggerConfiguration中归档的typeName并且似乎我只能通过 AppConfig 文件这样做,如果我要更改它,这又是一次事件,更改将影响所有记录器实例。
我错过了什么吗?
解决方案
更新的答案
要将属性和值添加到您的记录器,您可以使用Contextual logging 和 Enrichment
上下文记录器
将上下文属性附加到日志事件的最简单和最直接的方法
首先初始化您的记录器:
Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger();
然后你可以创建你的上下文记录器:
// adding Log Context
var StudentLogger = Log.Logger.ForContext<Student>();
StudentLogger.Error(/* log message */);
或者您可以使用相关日志条目:
// correlation Log Entries
var orderId = "some value";
var corrLog = Log.Logger.ForContext("orderId", orderId)
corrLog.Error(/* log message */);
丰富
在某些情况下,我们希望记录器创建的每个事件都带有相同的固定属性值。应用示例就是其中之一。
Serilog 为此在 LoggerConfiguration 级别提供 Enrich.WithProperty():
Log.Logger = new LoggerConfiguration()
.Enrich.WithProperty("Application", "e-Commerce")
.Enrich.WithProperty("Environment", ConfigurationManager.AppSettings["Environment"])
// Other logger configuration
原始答案
Serilog有两种配置方式:
使用 API(需要 serilog.sinks.elasticsearch 包):
var loggerConfig = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200") ){
AutoRegisterTemplate = true,
});
var logger = loggerConfig.CreateLogger();
使用 AppSettings中的配置(除了 serilog.sinks.elasticsearch 还需要 Serilog.Settings.AppSettings)
这样,您将所有设置都放在 AppSetting 文件中,例如
<appSettings>
<add key="serilog:using" value="Serilog.Sinks.Elasticsearch"/>
<add key="serilog:write-to:Elasticsearch.nodeUris" value="http://localhost:9200;http://remotehost:9200"/>
<add key="serilog:write-to:Elasticsearch.indexFormat" value="custom-index-{0:yyyy.MM}"/>
<add key="serilog:write-to:Elasticsearch.templateName" value="myCustomTemplate"/>
</appSettings>
并告诉 serilog 从 appSettigns 读取配置
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
... // Other configuration here, then
.CreateLogger()
请参阅:AppSetting和ElasticSearch 配置接收器
我不确定您指的是哪种日志事件类型?就我而言,我在记录错误时传递了对象类型:
catch (Exception ex)
{
Logger.Error(ex, string.Format("Exception occured in Controller: {0}, Action: Post.", this.GetType()), this.GetType());
推荐阅读
- java - Struts 2.5 运行 Junit 4 测试并在查找 Struts 插件父包时遇到运行时错误
- java - 按文件打开程序
- scala - Spark:将多列分解为一列
- python - 在页面源 selenium python 中查找某些文本
- pandas - 有效地从具有 groupby 和时间条件的 Pandas DataFrame 中删除行?
- android-layout - ListPreference 列表选项文本颜色
- wcf - 通过 WCF 服务上传大于 8 MB 的流
- vue.js - 通过事件总线 -Vue js 呈现的项目
- permissions - 如何解决此错误消息?Azure DevOPs TF401232:工作项不存在,或者您没有读取它的权限
- assembly - MIPS 中的运行时异常地址超出范围