AppSettings中的Serilog表达模板。

发布于 2025-02-09 14:59:04 字数 1754 浏览 2 评论 0 原文

我是Serilog的新手,并且正在尝试在我的 AppSettings.development.json 文件中使用表达模板语法。

在我的 program.cs 类I类中,我从代码初始化引导记录器。这是按预期工作的,使用“正常”输出模板记录到控制台。在 startup.cs 类I类中,I类使用 readfrom.configuration(...)。createlogger()

如果我的配置看起来如下,它可以正常工作,并使用指定的输出板将其记录到控制台:

"WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {EnvironmentName:u3} [{Level:u3}] {Message:lj} {Resource}{NewLine}{Exception}",
      "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
    }
  },...]

serilog“使用”标签(配置)为:

"Using": [
  "Serilog.Sinks.Console",
  "Serilog.Sinks.Seq",
  "Serilog.Expressions"
],

如果我更改 writeTo 标签标签,则为遵循日志格式未经尊重,并且使用默认模板(实际上是使用Bootstrap记录器配置的模板):

 "WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "formatter": {
        "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
        "template": "[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}] {@m}\n{@x}"
      }
    }
  },...]

在尝试解决解决方案时,我查看了Mberube.net here ,但是复制并粘贴了该解决方案中提供的代码,但仍未使用登录模板格式。迄今为止,我的研究表明,我使用的相关Serilog软件包的版本应该是正确的,以实现此功能。

我的应用程序是ASP .NET Core Web API Targetting Net5.0 。我已经引用了 serilog.expressions版本=“ 3.4.0” serilog.settings.configuration =“ 3.3.0”

我希望有人可以帮助我了解为什么没有应用模板的输出格式,以及我能做什么来纠正它?

如果需要进一步的信息,请告诉我。

谢谢

I am new to Serilog and am trying to use the Expression Template syntax within my appSettings.Development.json file.

In my Program.cs class I initialise the bootstrap logger from code. This works as expected, logging to the console with a 'normal' output template. Inside the Startup.cs class I re-setup the logger with ReadFrom.Configuration(...).CreateLogger().

If my configuration looks as follows, it works correctly and logs to the console using the specified outputTemplate:

"WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {EnvironmentName:u3} [{Level:u3}] {Message:lj} {Resource}{NewLine}{Exception}",
      "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
    }
  },...]

The Serilog "using" tag, common to the configuration, is:

"Using": [
  "Serilog.Sinks.Console",
  "Serilog.Sinks.Seq",
  "Serilog.Expressions"
],

If I change the WriteTo tag to be as follows the log format is not honoured and the default template is used (the template configured with the bootstrap logger in fact):

 "WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "formatter": {
        "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
        "template": "[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}] {@m}\n{@x}"
      }
    }
  },...]

In trying to work out the solution I have looked at the question posed by mberube.net here but copying and pasting the code provided in that solution still doesn't log using the template format. My research to date indicates that the versions of the relevant Serilog packages I am using should be correct to enable this functionality.

My application is an ASP .Net Core Web API targetting net5.0. I have referenced Serilog.Expressions Version="3.4.0" and Serilog.Settings.Configuration Version="3.3.0".

I am hoping someone can assist me to understand why the templated output format is not being applied and what I can do to correct it?

If there is any further information that is required please let me know.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

德意的啸 2025-02-16 14:59:05

appSettings.development.json 文件中的块中的块不 Override AppSettings.json - 它们是它们在< /em>,因此您最终最终的最终结果是:

 "WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {EnvironmentName:u3} [{Level:u3}] {Message:lj} {Resource}{NewLine}{Exception}",
      "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
      "formatter": {
        "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
        "template": "[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}] {@m}\n{@x}"
      }
    }
  }

这对于Serilog的配置过载选择不佳 - 它将选择它认为是可能的配置方法中最简单的选项。

一个更好的想法是在root appSettings.json 文件中没有接收器配置(“ WriteTo”块),然后将单独的配置放在 AppSettings.deferfect.json.json AppSettings.production.json 。否则,很难最终取得意外结果。

The blocks in appsettings.Development.json files don't override the ones in appsettings.json - they're merged in, so what you're most likely ending up with is:

 "WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {EnvironmentName:u3} [{Level:u3}] {Message:lj} {Resource}{NewLine}{Exception}",
      "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
      "formatter": {
        "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
        "template": "[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}] {@m}\n{@x}"
      }
    }
  }

This won't play well with Serilog's configuration overload selection - it'll pick what it thinks is the simplest option among possible configuration methods.

A better idea is to have no sink configuration ("WriteTo" blocks) in the root appsettings.json file at all, and put separate configurations in appsettings.Development.json and appsettings.Production.json. It's very hard not to end up with unexpected results, otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文