Serilog无法通过asp.net Core 2.2 API中的配置工作

编程入门 行业动态 更新时间:2024-10-25 09:28:40
本文介绍了Serilog无法通过asp Core 2.2 API中的配置工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); }) .UseStartup<Startup>(); } public class Startup { public IContainer Container { get; private set; } public Startup(IConfiguration configuration) { Log.Warning("test"); Configuration = configuration; } }

appsettings.json

appsettings.json

{ "Serilog": { "MinimumLevel": { "Default": "Information", "Override": { "Default": "Information", "Microsoft": "Information", "System": "Information" } }, "WriteTo": [ { "Name": "RollingFile", "Args": { "pathFormat": "C:\\test.txt", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.ffff}|{TenantName}|{RequestId}|{SourceContext}|{Level:u3}|{Message:lj}{NewLine}{Exception}", "restrictedToMinimumLevel": "Information" } } ] }, "AllowedHosts": "*" }

我已经安装了所有软件包

I have all the packages installed

  • Serilog,
  • Serilog.AspCore,
  • Serilog.Settings.Configuration,
  • Serilog.Sink.File
推荐答案

您的配置用于RollingFile,但您的软件包列表显示Serilog.Sinks.File.这些是不同的.您需要添加Serilog.Sinks.RollingFile程序包,它应该开始工作.

Your config is for RollingFile but your package list says Serilog.Sinks.File. These are different. You need to add the Serilog.Sinks.RollingFile package and it should start working.

如果要使用File接收器(如评论中提到的@Kirk,现在建议使用该选项),则需要将设置更改为

If you want to use the File sink (which, as @Kirk mentioned in the comments, is the recommended option now) then you need to change the settings to

"WriteTo": [ { "Name": "File", "Args": { "path": "C:\\test.txt", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.ffff}|{TenantName}|{RequestId}|{SourceContext}|{Level:u3}|{Message:lj}{NewLine}{Exception}", "restrictedToMinimumLevel": "Information" } }

注意

对于File接收器,pathFormat应该为path

请参见 Serilog文件接收器文档

更多推荐

Serilog无法通过asp.net Core 2.2 API中的配置工作

本文发布于:2023-11-17 03:29:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   net   asp   Serilog   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!