前言
什么是结构化呢? 结构化,就是将原本没有规律的东西进行有规律话。
就比如我们学习数据结构,需要学习排序然后又要学习查询,说白了这就是一套,没有排序,谈如何查询是没有意义的,因为查询算法就是根据某种规律得到最佳的效果。
同样日志结构话,能够让我们得到一些好处。如果说容易检索,容易分析,总的来说就是让我们的日志更加有规律。
如果我们的日志结构化了,那么可以使用elasticsearch 这样的框架进行二次整理,再借助一些分析工具。
我们就能做到可视化分析系统的运行情况,做到日志告警、上下文关联、实现追踪系统集成,同样也易于检索相关信息。
说了这么多,其实对于程序员,就是为了节约排查错误的时间,然后在做系统稳定化方案的时候有依据,我们是讲道理的,做事要有依据,不能张口就来。
正文
这里结构化,借助需要借助serilog,引用serilog.aspnetcore。
开源地址如下:
https://github.com/serilog/serilog-aspnetcore
这里介绍一下简单的接入方式:
在CreateHostBuilder 中加入UseSerilog:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}).UseSerilog(dispose:true);
然后在Startup的ConfigureServices的加入:
services.AddLogSeriLog(Configuration);
这个AddLogSeriLog 是我写的扩展:
public static class SeriLogExtension
{
public static Serilog.ILogger AddLogSeriLog(this IServiceCollection services, IConfiguration configuration)
{
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration)
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console(new RenderedCompactJsonFormatter())
.WriteTo.File(formatter: new CompactJsonFormatter(), "logs\\test.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
return Log.Logger;
}
}
输出:
这时候发现我们的输出都json化了。
同时在根目录下的logs目录下输出日志文件:
但是我们这样发现,日志太多了,我们需要过滤掉一些日志,那么我们可以配置:
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Error",
"System": "Information"
}
}
}
测试代码:
[HttpGet]
public int GetService([FromServices]ISelfService selfService)
{
_logger.LogInformation("Hello Word");
return 1;
}
结果:
因为上面配置了Microsoft 为Error级别的,故而系统打印的日志只有Error基本才会输出。
这里面简单看一下Serilog 的原理,
因为//www.ogeek.net/article/215119.htm已经介绍了日志系统的基本结果,故而直接往下续:
查看UseSerilog:
public static IHostBuilder UseSerilog(
this IHostBuilder builder,
ILogger logger = null,
bool dispose = false,
LoggerProviderCollection providers = null)
{
if (builder == null)
throw new ArgumentNullException(nameof (builder));
builder.ConfigureServices((Action<HostBuilderContext, IServiceCollection>) ((_, collection) =>
{
if (providers != null)
ServiceCollectionServiceExtensions.AddSingleton<ILoggerFactory>(collection, (Func<IServiceProvider, M0>) (services =>
{
SerilogLoggerFactory serilogLoggerFactory = new SerilogLoggerFactory(logger, dispose, providers);
foreach (ILoggerProvider service in (IEnumerable<ILoggerProvider>) ServiceProviderServiceExtensions.GetServices<ILoggerProvider>(services))
serilogLoggerFactory.AddProvider(service);
return (ILoggerFactory) serilogLoggerFactory;
}));
else
ServiceCollectionServiceExtensions.AddSingleton<ILoggerFactory>(collection, (Func<IServiceProvider, M0>) (services => (ILoggerFactory) new SerilogLoggerFactory(logger, dispose, (LoggerProviderCollection) null)));
SerilogHostBuilderExtensions.ConfigureServices(collection, logger);
}));
return builder;
}
这里我简单梳理一下日志系统的基本结构,首选是ILoggerFactory, 里面会有一个CreateLogger,创建一个ILogger。那么这个ILogger的作用是什么呢?
是用来统一接口的。 比如说我们有文件日志,有控制台日志,有很多方式输出日志。那么就要有一个管理的来统一接口。
而每一种打印方式,继承ILoggerProvider,比如ConsoleProvider继承ILoggerProvider有一个CreateLogger方法,这个CreateLog才是创建具体的实现类,比如说ConsoleLogger,ConsoleLogger又继承ILogger。
从而ILogger的另一个实现类Logger实现代理,管理其他继承ILogger的实现类。
可能我这样说有点模糊,那么请看完上面链接,应该就会明白我所以表达的意思。
那么回到上文中。
ServiceCollectionServiceExtensions.AddSingleton(collection, (Func<IServiceProvider, M0>) (services => (ILoggerFactory) new SerilogLoggerFactory(logger, dispose, (LoggerProviderCollection)
将我们的ILoggerFactory 替换成了SerilogLoggerFactory,官方是LoggerFactory。
那么看下SerilogLoggerFactory:
public SerilogLoggerFactory(
Serilog.ILogger logger = null,
bool dispose = false,
LoggerProviderCollection providerCollection = null)
{
this._provider = new SerilogLoggerProvider(logger, dispose);
this._providerCollection = providerCollection;
}
/// <summary>Disposes the provider.</summary>
public void Dispose()
{
this._provider.Dispose();
}
/// <summary>
/// Creates a new <see cref="T:Microsoft.Extensions.Logging.ILogger" /> instance.
/// </summary>
/// <param name="categoryName">The category name for messages produced by the logger.</param>
/// <returns>
/// The <see cref="T:Microsoft.Extensions.Logging.ILogger" />.
/// </returns>
public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName)
{
return this._provider.CreateLogger(categoryName);
}
/// <summary>
/// Adds an <see cref="T:Microsoft.Extensions.Logging.ILoggerProvider" /> to the logging system.
/// </summary>
/// <param name="provider">The <see cref="T:Microsoft.Extensions.Logging.ILoggerProvider" />.</param>
public void AddProvider(ILoggerProvider provider)
{
if (provider == null)
throw new ArgumentNullException(nameof (provider));
if (this._providerCollection != null)
this._providerCollection.AddProvider(provider);
else
SelfLog.WriteLine("Ignoring added logger provider {0}", (object) provider, (object) null, (object) null);
}
}
那么继续查看CreateLogger,SerilogLoggerProvider的CreateLogger 及其相关:
private readonly Serilog.ILogger _logger;
public SerilogLoggerProvider(Serilog.ILogger logger = null, bool dispose = false)
{
if (logger != null)
this._logger = logger.ForContext((IEnumerable<ILogEventEnricher>) new SerilogLoggerProvider[1]
{
this
});
if (!dispose)
return;
if (logger != null)
this._dispose = (Action) (() =>
{
if (!(logger is IDisposable disposable))
return;
disposable.Dispose();
});
else
this._dispose = new Action(Log.CloseAndFlush);
}
public Microsoft.Extensions.Logging.ILogger CreateLogger(string name)
{
return (Microsoft.Extensions.Logging.ILogger) new SerilogLogger(this, this._logger, name);
}
而SerilogLogger 就是具体来管理全部继承ILogger具体的实现的类。 那么是否是和官方一样,其他实现类都是继承自ILogger的呢?答案不是,他们都继承自ILogEventSink,下面会继续提及,暂时不要关心这个。
同时要忘记官方每个实现具体打印的类继承ILogger,它的设计思想还是一样的,只是换了一个继承接口。
SerilogLogger 里面继承Microsoft.Extensions.Logging.ILogger,实现具体的打印的。
看下SerilogLogger 的具体的部分,主要看log方法,因为这个实现打印的,这下面不用看太多,只需要看到其实里面是调用_logger的方法就行。
internal class SerilogLogger : Microsoft.Extensions.Logging.ILogger
{
private static readonly MessageTemplateParser MessageTemplateParser = new MessageTemplateParser();
private static readonly LogEventProperty[] LowEventIdValues = Enumerable.Range(0, 48).Select<int, LogEventProperty>((Func<int, LogEventProperty>) (n => new LogEventProperty("Id", (LogEventPropertyValue) new ScalarValue((object) n)))).ToArray<LogEventProperty>();
private readonly SerilogLoggerProvider _provider;
private readonly Serilog.ILogger _logger;
public SerilogLogger(SerilogLoggerProvider provider, Serilog.ILogger logger = null, string name = null)
{
SerilogLoggerProvider serilogLoggerProvider = provider;
if (serilogLoggerProvider == null)
throw new ArgumentNullException(nameof (provider));
this._provider = serilogLoggerProvider;
this._logger = logger;
Serilog.ILogger logger1 = this._logger;
if (logger1 == null)
logger1 = Serilog.Log.Logger.ForContext((IEnumerable<ILogEventEnricher>) new SerilogLoggerProvider[1]
{
provider
});
this._logger = logger1;
if (name == null)
return;
this._logger = this._logger.ForContext("SourceContext", (object) name, false);
}
public bool IsEnabled(LogLevel logLevel)
{
return this._logger.IsEnabled(LevelConvert.ToSerilogLevel(logLevel));
}
public IDisposable BeginScope<TState>(TState state)
{
return this._provider.BeginScope<TState>(state);
}
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter)
{
LogEventLevel serilogLevel = LevelConvert.ToSerilogLevel(logLevel);
if (!this._logger.IsEnabled(serilogLevel))
return;
Serilog.ILogger logger = this._logger;
string str1 = (string) null;
List<LogEventProperty> logEventPropertyList = new List<LogEventProperty>();
if (state is IEnumerable<KeyValuePair<string, object>> keyValuePairs)
{
foreach (KeyValuePair<string, object> keyValuePair in keyValuePairs)
{
if (keyValuePair.Key == "{OriginalFormat}" && keyValuePair.Value is string str2)
str1 = str2;
else if (keyValuePair.Key.StartsWith("@"))
{
LogEventProperty property;
if (logger.BindProperty(keyValuePair.Key.Substring(1), keyValuePair.Value, true, out property))
logEventPropertyList.Add(property);
}
else
{
LogEventProperty property;
if (logger.BindProperty(keyValuePair.Key, keyValuePair.Value, false, out property))
logEventPropertyList.Add(property);
}
}
Type type = state.GetType();
TypeInfo typeInfo = type.GetTypeInfo();
if (str1 == null && !typeInfo.IsGenericType)
{
str1 = "{" + type.Name + ":l}";
LogEventProperty property;
if (logger.BindProperty(type.Name, SerilogLogger.AsLoggableValue<TState>(state, formatter), false, out property))
logEventPropertyList.Add(property);
}
}
if (str1 == null)
{
string propertyName = (string) null;
if ((object) state != null)
{
propertyName = "State";
str1 = "{State:l}";
}
else if (formatter != null)
{
propertyName = "Message";
str1 = "{Message:l}";
}
LogEventProperty property;
if (propertyName != null && logger.BindProperty(propertyName, SerilogLogger.AsLoggableValue<TState>(state, formatter), false, out property))
logEventPropertyList.Add(property);
}
if (eventId.Id != 0 || eventId.Name != null)
logEventPropertyList.Add(SerilogLogger.CreateEventIdProperty(eventId));
MessageTemplate messageTemplate = SerilogLogger.MessageTemplateParser.Parse(str1 ?? "");
LogEvent logEvent = new LogEvent(DateTimeOffset.Now, serilogLevel, exception, messageTemplate, (IEnumerable<LogEventProperty>) logEventPropertyList);
logger.Write(logEvent);
}
}
那么SerilogLogger 的_logger 到底是什么呢?我们可以看到其实这个_logger 是SerilogLoggerFactory到SerilogLoggerProvider到SerilogLogger一层一层传进去的,最后调用ForContext生成,上面可以看到具体各个的实例化函数。
如果我们不传的话,那么会默认使用 Serilog.Log.Logger,在SerilogLogger的实例化函数中这样写道:
Serilog.ILogger logger1 = this._logger;
if (logger1 == null)
logger1 = Serilog.Log.Logger.ForContext((IEnumerable<ILogEventEnricher>) new SerilogLoggerProvider[1]
{
provider
});
this._logger = logger1;
而在UseSerilog中logger参数的参数介绍中也写道: The Serilog logger; if not supplied, the static will be used.
之所以重点谈论这个_logger 这个数据,是为了引出Serilog.Log.Logger这个东西,是不是感觉特别眼熟?我们在Startup的ConfigureServices中写道:
services.AddLogSeriLog(Configuration);
AddLogSeriLog 为:
public static Serilog.ILogger AddLogSeriLog(this IServiceCollection services, IConfiguration configuration)
{
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration)
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console(new RenderedCompactJsonFormatter())
.WriteTo.File(formatter: new CompactJsonFormatter(), "logs\\test.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
return Log.Logger;
}
这里我们就生成了具体打印实现类的管理类,是代理模式哈。大体就是各个不同的实现类继承ILogEventSink,然后SafeAggregateSink也继承自ILogEventSink,SafeAggregateSink里面有个属性readonly ILogEventSink[] _sinks,然后调用Serilog的 Logger
类调用write方法会调用SafeAggregateSink的Emit。
SafeAggregateSink类如下,主要看下Emit方法:
class SafeAggregateSink : ILogEventSink
{
readonly ILogEventSink[] _sinks;
public SafeAggregateSink(IEnumerable<ILogEventSink> sinks)
{
if (sinks == null) throw new ArgumentNullException(nameof(sinks));
_sinks = sinks.ToArray();
}
public void Emit(LogEvent logEvent)
{
foreach (var sink in _sinks)
{
try
{
// 调用不同的Log打印的实现类 如文件、控制台 等
sink.Emit(logEvent);
}
catch (Exception ex)
{
SelfLog.WriteLine("Caught exception while emitting to sink {0}: {1}", sink, ex);
}
}
}
}
具体就不细写了,里面都是一些格式匹配的,根据我们上面的分析,其实我们应该知道先看CreateLogger这个函数哈,然后去看Console这个函数。如对格式化感兴趣可
以去看下哈,里面又套了一层代理模式的娃。
以上就是.net core日志结构化相关总结的详细内容,更多关于.net core日志结构化的资料请关注极客世界其它相关文章! |
请发表评论