Serilog,支持對象,把log數據序列化成Json,好用方便,容易拓展。Github: https://github.com/handsomeyao77/serilog-sinks-elasticsearchgit
配置文件分爲App.config和appsetting.json兩種。
讀取appsettings.json,主要是Web app 類型,首先在服務啓動時要注入json文件:
github
讀取配置:
json
配置App.config,高亮部分是必要的key:
app
讀取配置:
elasticsearch
固然,有時候能夠Hard code,這裏分享一種方式:
工具
1.Information.
ui
2.Debug.
3d
3.Error.
code
由於LogEventBase做爲基類,能夠繼承拓展一下。orm
public class LogEventBase { [FieldOrder(0)] public LogEventType EventType { get; set; } [FieldOrder(1)] public string Role { get; set; } [FieldOrder(2)] public string RoleInstance { get; set; } [FieldOrder(3)] public string Ip { get; set; } [FieldOrder(4)] public int Port { get; set; } [FieldOrder(5)] public DateTime Timestamp => DateTime.UtcNow; [FieldOrder(6)] public string Message { get; set; } [FieldOrder(7)] public IActivity Activity { get; set; } [FieldOrder(8)] public string EnvironmentName => Environment.MachineName; }
根據業務拓展:
public class LatencyEvent : LogEventBase { [FieldOrder(9)] public long Latency { get; set; } [FieldOrder(10)] public string SearchId { get; set; } } public class SearchEvent : LogEventBase { [FieldOrder(9)] public string SearchId { get; set; } [FieldOrder(10)] public string SearchString { get; set; } } public class LuisEvent : LogEventBase { [FieldOrder(9)] public LuisResult LuisResult { get; set; } } public class ExceptionEvent : LogEventBase { [FieldOrder(10)] public Exception Exception { get; set; } }
LogEventType是比較實用的,在kibana查看log是做爲過濾條件能節省不少時間。
打開kibana主頁,選擇對應的template,能夠說很是好用的log工具。能夠按照log的時間,或者輸入查詢條件:
或者按照LogEventType,能夠快速定位到target: