Objective
Serilog, support objects, the log data serialized into JSON, easy to use, easily expand. Github:https://github.com/handsomeyao77/serilog-sinks-elasticsearch
Reading the configuration file
The configuration file is divided into app. Config and Appsetting.json two types.
Read Appsettings.json, primarily web app types, first inject the JSON file when the service starts:
Read configuration:
To configure App. Config, the highlighted part is the necessary key:
Read configuration:
Of course, sometimes you can hard code, and here's a way to share:
Common Types of Serilog
1.Information.
2.Debug.
3.Error.
Because Logeventbase as a base class, you can inherit and expand.
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;}
Based on business development:
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 is more practical, and viewing log in Kibana can save a lot of time as a filtering condition.
How to use 1.Information
2.Error
Kibana
Open the Kibana homepage and select the corresponding template, which can be said to be a very useful log tool. You can follow the log time, or enter the query criteria:
Alternatively, you can quickly locate the target by following logeventtype:
. Net Core's log mode: Serilog+kibana