![](/img/trans.png)
[英]Is it possible to disabled the reflection based destructurer in Serilog.Exceptions?
[英]Serilog.Exceptions Rolling Files
記錄新異常時,將創建一個新文件。
場景:創建的第一個文件:
日志-20170602
根據例外創建的文件:
日志-20170602_001
日志-20170602_002
日志-20170602_003
我的代碼(C#-Visual Studio 2015項目):
public class EventLogging
{
private readonly Logger _logger;
public EventLogging(IOptions<LoggingOptions> logOption)
{
var logAccessor = logOption.Value;
_logger = new LoggerConfiguration()
.Enrich.WithExceptionDetails()
.WriteTo.Sink(new RollingFileSink(logAccessor.Path,
new JsonFormatter(renderMessage: true), null, null)).CreateLogger();
}
public void LogError(string message, Exception exception)
{
_logger.Error(exception, message);
}
public void LogWarning(string message, Exception exception)
{
_logger.Warning(exception, message);
}
}
引用該類以避免在每個類中編寫以下代碼:
_logger = new LoggerConfiguration()
.Enrich.WithExceptionDetails()
.WriteTo.Sink(new RollingFileSink(logAccessor.Path,
new JsonFormatter(renderMessage: true), null, null)).CreateLogger();
有沒有一種方法可以防止按例外但按新日期創建文件?
您是否在每次記錄內容時都創建一個新的記錄器? 通常,我使用Serilog初始化一次Singleton Log實例,並在各處使用它:
在啟動時:
Serilog.Log.Logger = new LoggerConfiguration()
.Enrich.WithExceptionDetails()
.WriteTo.Sink(new RollingFileSink(logAccessor.Path,
new JsonFormatter(renderMessage: true), null, null)).CreateLogger();
后來:
Serilog.Log.Error(exception, message);
就您而言,我認為如果只保留_logger
的靜態實例,而不是_logger
創建一個新實例,它將解決您的問題。 或者,如果僅保留相同的RollingFileSink
用它,則可以使用其他記錄器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.