如今,我正在开发.Net核心工作程序服务,我想将项目的错误保存在文件(文本文件)中,但我找不到此设置
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddEventLog(new EventLogSettings()
{
SourceName = "CallerTestCacheServiceCoreAPI",
LogName = "CallerTestCacheServiceCoreAPILog",
});
})
我在另一个类似该项目的类中添加了扩展方法https://github.com/MV10/Serilog.Dependency.Injection
public static IServiceCollection AddSerilogServices(this IServiceCollection services)
{
return services.AddSerilogServices(
new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.File("log.txt")
//.WriteTo.File(new CompactJsonFormatter(), "log.txt")
.WriteTo.Console());
}
并将其引入program.cs服务中
services.AddSerilogServices();
在我的工人班上:
readonly Serilog.ILogger _log;
public Worker( Serilog.ILogger log)
{
_log = log;
log.Information("Hello World!");
}
而且有效
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句