[英]Serilog ASP .Net Core not writing logs to file in MacOS but works in Windows
[英]Writing logs into Excel file using Serilog for .NET
我已经使用Serilog
在我的 WPF 应用程序中实现了logging
。 我希望生成的输出为Excel
格式。 我希望excel
文件具有如下所述的这些column headers
,以便我可以通过应用filters
进行sort
。
date time| logtype |environment| app build version | test case description | status
示例output
应如下所示
date time | logtype |environment| app build version| test case description | status
02-04-2020 4:30 | Test Reults|aBC06 |2.0.150 | Loading Views | Success
我有以下logging
配置
public class LoggerFactory : ILoggerFactory
{
public Serilog.Core.Logger Create()
{
var logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.CreateLogger();
return logger;
}
}
AppSettings
有这个配置
<add key="serilog:using:Seq" value="Serilog.Sinks.Seq" />
<add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
<add key="serilog:write-to:RollingFile.pathFormat" value="C:\Dev\Logs\abc-ui-automation-{Date}.txt" />
<add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="10" />
<add key="serilog:write-to:Seq.serverUrl" value="http://localhost:5341" />
目前, logger
正在写入没有上述格式的txt
文件。 我如何确保完成上述任务?
最简单的解决方案是将数据记录为 CSV,然后用 excel 打开它。 因此,您可以简单地实现您自己的ITextFormatter版本。 检查默认实现,如RawFormatter以了解如何。
您只需要编写自己的实现,例如
public void Format(LogEvent logEvent, TextWriter output)
{
output.write(logEvent.Timestamp.ToString("dd-MM-yyyy H:mm");
output.write(";");
output.write(logEvent.Level);
output.write(";");
output.write(logEvent.Properties["KEY"]);
output.write(";");
//...
output.WriteLine();
}
要编写标题,您可以使用Serilog.Sinks.File.Header包。 基本上它可以像
Func<string> headerFactory = () => "date time;logtype;...";
Log.Logger = new LoggerConfiguration()
.WriteTo.File(new YourCsvFormatter(), "log.csv", hooks: new HeaderWriter(headerFactory))
.CreateLogger();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.