C# 使用Serilog日志框架的方法
Serilog是一款配置方便,使用靈活的日志框架,使用方法如下:
1、日志輸出到控制臺,需要使用Nuget安裝Serilog和Serilog.Sinks.Console兩個包
// 初始化日志的共享實例
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
// 寫入日志
Log.Information("Info");2、日志輸出到文件,需要安裝Serilog.Sinks.File包
// 初始化日志的共享實例
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.File("logs/app.log",
rollingInterval: RollingInterval.Day, // 每天一個日志文件
shared: true // 允許其他進(jìn)程共享日志文件
).CreateLogger();
// 寫入日志
Log.Information("Info");3、可以通過配置,讓不同日志級別輸出到不同的日志文件
// 初始化日志的共享實例
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
// 調(diào)試日志
.WriteTo.Logger(x => x
.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Debug)
.WriteTo.File("logs/debug.log", rollingInterval: RollingInterval.Day, shared: true)
// 錯誤日志
).WriteTo.Logger(x => x
.Filter.ByIncludingOnly(e => e.Level == Serilog.Events.LogEventLevel.Error)
.WriteTo.File("logs/error.log", rollingInterval: RollingInterval.Day, shared: true)
// 應(yīng)用日志
).WriteTo.Logger(x => x
.Filter.ByIncludingOnly(e => e.Level != Serilog.Events.LogEventLevel.Debug &&
e.Level != Serilog.Events.LogEventLevel.Error)
.WriteTo.File("logs/app.log", rollingInterval: RollingInterval.Day, shared: true)
).CreateLogger();
// 寫入日志到 app.log
Log.Information("Info");
// 寫入日志到 debug.log
Log.Debug("Debug");
// 寫入日志到 error.log
Log.Error("Error");4、寫入日志到SqlServer
// 寫入數(shù)據(jù)庫的配置
var sinkOpts = new MSSqlServerSinkOptions();
sinkOpts.TableName = "logs"; // 日志表名
sinkOpts.AutoCreateSqlTable = true; // 自動創(chuàng)建表
// 列配置
var columnOpts = new ColumnOptions();
columnOpts.Store.Remove(StandardColumn.Exception); // 去掉異常列
columnOpts.Store.Remove(StandardColumn.Properties); // 去掉屬性列
columnOpts.Exception.DataLength = 2048; // 指定列的長度
columnOpts.TimeStamp.NonClusteredIndex = true; // 去掉時間戳的聚集索引
// 初始化日志的共享實例
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.MSSqlServer(
connectionString: ConfigurationManager.ConnectionStrings["Default"].ConnectionString,
sinkOptions: sinkOpts,
columnOptions: columnOpts
).CreateLogger();
// 寫入日志
Log.Information("Info");執(zhí)行代碼后,發(fā)現(xiàn)日志表并沒有添加到數(shù)據(jù)庫,也沒有任何異常信息,我們可以添加以下代碼調(diào)試Serilog
Serilog.Debugging.SelfLog.Enable(msg =>
{
Debug.Print(msg);
Debugger.Break();
});獲取到如下的錯誤信息:
2022-11-26T09:34:53.6107406Z Unable to write 1 log events to the database due to following error: A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 證書鏈?zhǔn)怯刹皇苄湃蔚念C發(fā)機(jī)構(gòu)頒發(fā)的。)
需要在連接字符串添加信任服務(wù)器證書的配置:TrustServerCertificate=True;
就能在SqlServer中創(chuàng)建日志表并寫入日志。
到此這篇關(guān)于C# 使用Serilog日志框架的文章就介紹到這了,更多相關(guān)C# Serilog日志框架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#代碼實現(xiàn)添加或刪除PowerPoint文檔中的節(jié)
這篇文章主要為大家詳細(xì)介紹了C#如何使用 Spire.Presentation for .NET 通過編程方式在 PowerPoint 文檔中添加或刪除節(jié),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2026-03-03
c# 服務(wù)器上傳木馬監(jiān)控代碼(包含可疑文件)
c# 監(jiān)控服務(wù)器上傳木馬(包含可疑文件)2010-05-05
Unity實現(xiàn)鼠標(biāo)或者手指點擊模型播放動畫
這篇文章主要為大家詳細(xì)介紹了Unity實現(xiàn)鼠標(biāo)或者手指點擊模型播放動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-01-01

