我在Asp点网核心应用程序中使用Serilog。我希望我的日志文件易于阅读,但也可以轻松解析。
我遇到的问题是使用换行符记录了异常。某些Microsoft事件具有包含换行符的消息。
我希望能够使用每行一个事件来解析日志文件。
我可以编写自己的ITextFormatter实现,用\ r \ n替换新行,但这意味着我需要复制MessageTemplateTextFormatter和其他类中的许多逻辑。
在研究了一段时间之后,我能够提出一个答案。安迪·韦斯特的答案为我指明了正确的方向。
这里有两个单独的问题:消息中的CRLF和异常中的CRLF。
通过将outputTemplate中的“ {Message}”更改为“ {Message:j}”,我能够解决消息问题。
更改例外情况有些棘手。我必须添加一个浓缩器:
class ExceptionEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (logEvent.Exception == null)
return;
var logEventProperty = propertyFactory.CreateProperty("EscapedException", logEvent.Exception.ToString().Replace("\r\n", "\\r\\n"));
logEvent.AddPropertyIfAbsent(logEventProperty);
}
}
Run Code Online (Sandbox Code Playgroud)
这将添加一个名为EscapedException的新属性。必须使用.Enrich.With()将其添加到配置中。
然后,我在outputTemplate中用“ {EscapedException}”替换了“ {Exception}”。
小智 5
此技术将删除所有 CRLF。首先是一个新的 ITextFormatter。
public class RemoveCrLf : ITextFormatter
{
private const int DefaultWriteBuffer = 256;
private readonly ITextFormatter _textFormatter;
/// <summary>
///
/// </summary>
/// <param name="textFormatter"></param>
public RemoveCrLf(ITextFormatter textFormatter)
{
_textFormatter = textFormatter;
}
/// <summary>
///
/// </summary>
/// <param name="logEvent"></param>
/// <param name="output"></param>
public void Format(LogEvent logEvent, TextWriter output)
{
var buffer = new StringWriter(new StringBuilder(DefaultWriteBuffer));
_textFormatter.Format(logEvent, buffer);
var logText = buffer.ToString();
output.WriteLine(logText.Trim().Replace("\n","\\n").Replace("\r","\\r"));
output.Flush();
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它
configuration.WriteTo.Console(new RemoveCrLf(new MessageTemplateTextFormatter("[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Exception}")));
Run Code Online (Sandbox Code Playgroud)
当然,根据需要自定义输出模板。
| 归档时间: |
|
| 查看次数: |
1091 次 |
| 最近记录: |