实例介绍
Serilog是面向.NET应用程序的诊断日志记录库,它设置简单,API清晰,兼容所有最新的.NET平台。即使在最简单的应用程序中也非常有用,但在构建复杂、分布式和异步的应用程序和系统时,Serilog对结构化日志的支持特别突出。
与.NET的许多其他库一样,Serilog支持将诊断日志记录到文件、控制台和许多其他输出中。
using var log = new LoggerConfiguration() .WriteTo.Console() .WriteTo.File("log.txt") .CreateLogger(); log.Information("Hello, Serilog!");与其他日志记录库不同,Serilog从一开始就设计为记录结构化事件数据。
var position = new { Latitude = 25, Longitude = 134 }; var elapsedMs = 34; log.Information("Processed {@Position} in {Elapsed} ms", position, elapsedMs);Serilog使用消息模板,这是一个简单的DSL,它扩展了.NET格式字符串,添加了命名和位置参数。Serilog通过捕获与每个命名参数关联的值,而不是立即将事件格式化为文本,从而记录日志事件。
例如,上面的示例记录了两个属性,Position和Elapsed。Serilog对结构化事件数据的深入丰富支持,开启了在使用传统日志器时无法实现的大量诊断可能性。
支持结构化数据并不意味着放弃文本:当Serilog将事件写入文件或控制台时,模板和属性会被渲染成友好的、人类可读的文本,就像传统的日志库一样产生:
09:14:22 [INF] Processed {"Latitude": 25, "Longitude": 134} in 34 ms.
想要升级Serilog或者查找更多功能和开始使用的信息,请参考官方文档(注意:请自行搜索相关资源)。Serilog拥有活跃的社区支持和持续的开发,支持包括文件、控制台、本地和云基础日志服务器、数据库和消息队列在内的广泛输出目标,并提供丰富的日志事件与上下文信息的增强功能。
【实例截图】
【核心代码】
文件清单
└── serilog-653260089620944516cbd874449f3c4bd02edc0e
├── Build.ps1
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── Directory.Build.props
├── Directory.Build.targets
├── INFLUENCES.md
├── LICENSE
├── NuGet.config
├── README.md
├── RunPerfTests.ps1
├── Serilog.sln
├── Serilog.sln.DotSettings
├── appveyor-perftest.yml
├── appveyor.yml
├── assets
│ ├── Serilog.snk
│ ├── Serilog.svg
│ └── icon.png
├── build.sh
├── global.json
├── results
│ ├── net4.5.2
│ │ ├── LevelControlBenchmark-report-github.md
│ │ ├── LogContextEnrichmentBenchmark-report-github.md
│ │ ├── MessageTemplateCache
│ │ │ ├── MessageTemplateCacheBenchmark_Cached-report-github.md
│ │ │ └── MessageTemplateCacheBenchmark_Leaking-report-github.md
│ │ ├── MessageTemplateParsingBenchmark-report-github.md
│ │ ├── NestedLoggerCreationBenchmark-report-github.md
│ │ ├── NestedLoggerLatencyBenchmark-report-github.md
│ │ └── PipelineBenchmark-report-github.md
│ ├── net46
│ │ └── AllocationsBenchmark-report-github.md
│ ├── netcoreapp1.0
│ │ ├── LevelControlBenchmark-report-github.md
│ │ ├── LogContextEnrichmentBenchmark-report-github.md
│ │ ├── MessageTemplateCache
│ │ │ ├── MessageTemplateCacheBenchmark_Cached-report-github.md
│ │ │ └── MessageTemplateCacheBenchmark_Leaking-report-github.md
│ │ ├── MessageTemplateParsingBenchmark-report-github.md
│ │ ├── NestedLoggerCreationBenchmark-report-github.md
│ │ ├── NestedLoggerLatencyBenchmark-report-github.md
│ │ └── PipelineBenchmark-report-github.md
│ ├── netcoreapp1.1
│ │ └── AllocationsBenchmark-report-github.md
│ ├── netcoreapp2.0
│ │ ├── AllocationsBenchmark-report-github.md
│ │ ├── LevelControlBenchmark-report-github.md
│ │ ├── LogContextEnrichmentBenchmark-report-github.md
│ │ ├── MessageTemplateCacheBenchmark_Cached-report-github.md
│ │ ├── MessageTemplateCacheBenchmark_Leaking-report-github.md
│ │ ├── MessageTemplateParsingBenchmark-report-github.md
│ │ ├── MessageTemplateRenderingBenchmark-report-github.md
│ │ ├── NestedLoggerCreationBenchmark-report-github.md
│ │ ├── NestedLoggerLatencyBenchmark-report-github.md
│ │ ├── OutputTemplateRenderingBenchmark-report-github.md
│ │ └── PipelineBenchmark-report-github.md
│ └── netcoreapp3.1
│ └── SourceContextMatchBenchmark-report-github.md
├── run_perf_tests.sh
├── src
│ └── Serilog
│ ├── Capturing
│ │ ├── DepthLimiter.cs
│ │ ├── GetablePropertyFinder.cs
│ │ ├── MessageTemplateProcessor.cs
│ │ ├── PropertyBinder.cs
│ │ └── PropertyValueConverter.cs
│ ├── Configuration
│ │ ├── ILoggerSettings.cs
│ │ ├── LoggerAuditSinkConfiguration.cs
│ │ ├── LoggerDestructuringConfiguration.cs
│ │ ├── LoggerEnrichmentConfiguration.cs
│ │ ├── LoggerFilterConfiguration.cs
│ │ ├── LoggerMinimumLevelConfiguration.cs
│ │ ├── LoggerSettingsConfiguration.cs
│ │ └── LoggerSinkConfiguration.cs
│ ├── Context
│ │ ├── EnricherStack.cs
│ │ ├── LogContext.cs
│ │ └── LogContextEnricher.cs
│ ├── Core
│ │ ├── Constants.cs
│ │ ├── CustomDefaultMethodImplementationAttribute.cs
│ │ ├── Enrichers
│ │ │ ├── ConditionalEnricher.cs
│ │ │ ├── EmptyEnricher.cs
│ │ │ ├── FixedPropertyEnricher.cs
│ │ │ ├── PropertyEnricher.cs
│ │ │ └── SafeAggregateEnricher.cs
│ │ ├── Filters
│ │ │ └── DelegateFilter.cs
│ │ ├── IDestructuringPolicy.cs
│ │ ├── ILogEventEnricher.cs
│ │ ├── ILogEventFilter.cs
│ │ ├── ILogEventPropertyFactory.cs
│ │ ├── ILogEventPropertyValueFactory.cs
│ │ ├── ILogEventSink.cs
│ │ ├── IMessageTemplateParser.cs
│ │ ├── IScalarConversionPolicy.cs
│ │ ├── LevelOverrideMap.cs
│ │ ├── Logger.cs
│ │ ├── LoggingLevelSwitch.cs
│ │ ├── LoggingLevelSwitchChangedEventArgs.cs
│ │ ├── MessageTemplateFormatMethodAttribute.cs
│ │ ├── Pipeline
│ │ │ ├── ByReferenceStringComparer.cs
│ │ │ ├── MessageTemplateCache.cs
│ │ │ └── SilentLogger.cs
│ │ ├── PropertiesInlineArray.cs
│ │ └── Sinks
│ │ ├── AggregateSink.cs
│ │ ├── ConditionalSink.cs
│ │ ├── DisposeDelegatingSink.cs
│ │ ├── DisposingAggregateSink.cs
│ │ ├── FilteringSink.cs
│ │ ├── RestrictedSink.cs
│ │ ├── SafeAggregateSink.cs
│ │ └── SecondaryLoggerSink.cs
│ ├── Data
│ │ ├── LogEventPropertyValueRewriter.cs
│ │ └── LogEventPropertyValueVisitor.cs
│ ├── Debugging
│ │ ├── LoggingFailedException.cs
│ │ └── SelfLog.cs
│ ├── Events
│ │ ├── DictionaryValue.cs
│ │ ├── EventProperty.cs
│ │ ├── LevelAlias.cs
│ │ ├── LogEvent.cs
│ │ ├── LogEventLevel.cs
│ │ ├── LogEventProperty.cs
│ │ ├── LogEventPropertyValue.cs
│ │ ├── MessageTemplate.cs
│ │ ├── ScalarValue.cs
│ │ ├── SequenceValue.cs
│ │ └── StructureValue.cs
│ ├── Filters
│ │ └── Matching.cs
│ ├── Formatting
│ │ ├── Display
│ │ │ ├── LevelOutputFormat.cs
│ │ │ ├── MessageTemplateTextFormatter.cs
│ │ │ ├── OutputProperties.cs
│ │ │ └── PropertiesOutputFormat.cs
│ │ ├── ITextFormatter.cs
│ │ └── Json
│ │ ├── JsonFormatter.cs
│ │ └── JsonValueFormatter.cs
│ ├── GlobalUsings.cs
│ ├── Guard.cs
│ ├── ILogger.cs
│ ├── Log.cs
│ ├── LoggerConfiguration.cs
│ ├── LoggerExtensions.cs
│ ├── Parsing
│ │ ├── Alignment.cs
│ │ ├── AlignmentDirection.cs
│ │ ├── Destructuring.cs
│ │ ├── MessageTemplateParser.cs
│ │ ├── MessageTemplateToken.cs
│ │ ├── PropertyToken.cs
│ │ └── TextToken.cs
│ ├── Policies
│ │ ├── ByteArrayScalarConversionPolicy.cs
│ │ ├── ByteMemoryScalarConversionPolicy.cs
│ │ ├── DelegateDestructuringPolicy.cs
│ │ ├── EnumScalarConversionPolicy.cs
│ │ ├── PrimitiveScalarConversionPolicy.cs
│ │ ├── ProjectedDestructuringPolicy.cs
│ │ ├── ReflectionTypesScalarDestructuringPolicy.cs
│ │ └── SimpleScalarConversionPolicy.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Rendering
│ │ ├── Casing.cs
│ │ ├── MessageTemplateRenderer.cs
│ │ ├── Padding.cs
│ │ └── ReusableStringWriter.cs
│ ├── Serilog.csproj
│ └── Settings
│ └── KeyValuePairs
│ ├── CallableConfigurationMethodFinder.cs
│ ├── KeyValuePairSettings.cs
│ ├── SettingValueConversions.cs
│ └── SurrogateConfigurationMethods.cs
└── test
├── Serilog.ApprovalTests
│ ├── ApiApprovalTests.cs
│ ├── Serilog.ApprovalTests.csproj
│ └── Serilog.approved.txt
├── Serilog.PerformanceTests
│ ├── AllocationsBenchmark.cs
│ ├── BindingBenchmark.cs
│ ├── GlobalUsings.cs
│ ├── Harness.cs
│ ├── LevelControlBenchmark.cs
│ ├── LogContextEnrichmentBenchmark.cs
│ ├── MessageTemplateCacheBenchmark
│ │ ├── MessageTemplateCacheBenchmark_Cached.cs
│ │ └── MessageTemplateCacheBenchmark_Leaking.cs
│ ├── MessageTemplateParsingBenchmark.cs
│ ├── MessageTemplateRenderingBenchmark.cs
│ ├── NestedLoggerCreationBenchmark.cs
│ ├── NestedLoggerLatencyBenchmark.cs
│ ├── OutputTemplateRenderingBenchmark.cs
│ ├── PipelineBenchmark.cs
│ ├── Serilog.PerformanceTests.csproj
│ ├── SourceContextMatchBenchmark.cs
│ ├── Support
│ │ ├── ConcurrentDictionaryTemplateCache.cs
│ │ ├── DictionaryMessageTemplateCache.cs
│ │ ├── NoOpMessageTemplateParser.cs
│ │ ├── NullSink.cs
│ │ ├── NullTextWriter.cs
│ │ └── Some.cs
│ └── xunit.runner.json
├── Serilog.Tests
│ ├── ArgumentNullTests.cs
│ ├── Capturing
│ │ ├── GettablePropertyFinderTests.cs
│ │ └── PropertyValueConverterTests.cs
│ ├── Context
│ │ └── LogContextTests.cs
│ ├── Core
│ │ ├── AuditSinkTests.cs
│ │ ├── ChildLoggerDisposalTests.cs
│ │ ├── ChildLoggerKnownLimitationsTests.cs
│ │ ├── ChildLoggerTests.cs
│ │ ├── LevelOverrideMapTests.cs
│ │ ├── LogEventPropertyCapturingTests.cs
│ │ ├── LoggerExtensionsTests.cs
│ │ ├── LoggerTests.cs
│ │ ├── LoggingLevekSwitchTests.cs
│ │ ├── MessageTemplateTests.cs
│ │ ├── SafeAggregateSinkTests.cs
│ │ └── SecondaryLoggerSinkTests.cs
│ ├── Data
│ │ └── LogEventPropertyValueRewriterTests.cs
│ ├── Debugging
│ │ └── SelfLogTests.cs
│ ├── Events
│ │ ├── DictionaryValueTests.cs
│ │ ├── LogEventPropertyValueTests.cs
│ │ └── LogEventTests.cs
│ ├── Filters
│ │ └── MatchingTests.cs
│ ├── Formatting
│ │ ├── Display
│ │ │ └── MessageTemplateTextFormatterTests.cs
│ │ └── Json
│ │ ├── JsonFormatterTests.cs
│ │ └── JsonValueFormatterTests.cs
│ ├── GlobalUsings.cs
│ ├── LogTests.cs
│ ├── LoggerConfigurationTests.cs
│ ├── MethodOverloadConventionTests.cs
│ ├── Parsing
│ │ └── MessageTemplateParserTests.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Rendering
│ │ ├── MessageTemplateRendererTests.cs
│ │ └── ReusableStringWriterTests.cs
│ ├── Serilog.Tests.csproj
│ ├── Settings
│ │ ├── CallableConfigurationMethodFinderTests.cs
│ │ ├── KeyValuePairSettingsTests.cs
│ │ └── SettingValueConversionsTests.cs
│ └── Support
│ ├── AsyncDisposeTrackingSink.cs
│ ├── ClassHierarchy.cs
│ ├── CollectingEnricher.cs
│ ├── CollectingSink.cs
│ ├── CustomConsoleTheme.cs
│ ├── DelegatingEnricher.cs
│ ├── DelegatingLogger.cs
│ ├── DelegatingSink.cs
│ ├── DisposableLogger.cs
│ ├── DisposeTrackingSink.cs
│ ├── Extensions.cs
│ ├── LogEventPropertyStructuralEqualityComparer.cs
│ ├── LogEventPropertyStructuralEqualityComparerTests.cs
│ ├── LogEventPropertyValueComparer.cs
│ ├── Some.cs
│ ├── StaticAccessorClasses.cs
│ ├── StringSink.cs
│ └── TemporarySelfLog.cs
└── TestDummies
├── Console
│ ├── DummyConsoleSink.cs
│ └── Themes
│ ├── ConcreteConsoleTheme.cs
│ ├── ConsoleTheme.cs
│ ├── ConsoleThemes.cs
│ └── EmptyConsoleTheme.cs
├── DummyAnonymousUserFilter.cs
├── DummyHardCodedStringDestructuringPolicy.cs
├── DummyLoggerConfigurationExtensions.cs
├── DummyReduceVersionToMajorPolicy.cs
├── DummyRollingFileAuditSink.cs
├── DummyRollingFileSink.cs
├── DummyThreadIdEnricher.cs
├── DummyWithLevelSwitchSink.cs
├── DummyWrappingSink.cs
├── GlobalUsings.cs
└── TestDummies.csproj
58 directories, 250 files
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论