在appsetting中配置Nlog写入Seq

发布于 2025-01-17 11:14:39 字数 3106 浏览 1 评论 0原文

我在 net core 中有一个应用程序,该应用程序将所有日志写入 SeqLog,我使用 Nlog 来配置所有内容,到目前为止,我已经使用 .xml 文件为 Nlog 提供对正确操作有用的所有信息。 浏览互联网我发现可以使用appsetting 而不是用 xml 编写的 .config 文件来声明所有设置。 现在我想删除这个 .config 文件并将所有内容写入 appsetting 中,所以我开始编写所有内容,除了一件小事之外它似乎可以工作: 这就是我的 .config 文件的样子:

<targets>
        <target name="seq" xsi:type="Seq" serverUrl="https://....." apiKey="....." >
            <property name="Application" value="Name Application" />
            <property name="MachineName" value="localhost" />
            <property name="Environment" value="TEST" />
            <property name="Body" value =" ${aspnet-request-posted-body}" />
            <property name="Source" value="${logger}" />
            <parameter name="@json" layout="${event-properties:item=JsonMessage}" />
            <parameter name="@error" layout="${event-properties:item=ErrorMessage}" />
        </target>
    </targets>
    <rules>
        <logger name="*" minlevel="Info" writeTo="seq" />
    </rules>

然后将所有内容传输到 appsetting,这就是它的样子:

"NLog": {
    "extensions": [
      { "assembly": "NLog.Targets.Seq" }
    ],
    "targets": {
      "seq": {
        "type": "Seq",
        "serverUrl": "https://.....",
        "apiKey": "........",
        "propertys": [
          {
            "name": "Application",
            "value": "Application_name"
          },
          {
            "name": "MachineName",
            "value": "localhost"
          },
          {
            "name": "Environment",
            "value": "TEST"
          },
          {
            "name": "Body",
            "value": "${aspnet-request-posted-body}"
          },
          {
            "name": "Source",
            "value": "${logger}"
          }
        ],
        "parameters": [
          {
            "name": "@json",
            "layout": "${event-properties:item=JsonMessage}"
          },
          {
            "name": "@error",
            "layout": "${event-properties:item=ErrorMessage}"
          }
        ]
      }   
  },
    "rules": [
      {
        "logger": "*",
        "minLevel": "Info",
        "writeTo": "seq"
      }
    ]
  },

问题是属性没有被读取,所以 seqlog 应该写给我,例如主体或机器的名称,不,我还没有在互联网上找到我声明属性和参数的方式是否正确,但此时我认为它不是,所以我想知道声明属性和参数的正确方式是什么应用程序设置?

难道也是Program.cs的配置问题? 这是我写的:

    var config = new ConfigurationBuilder()
        .SetBasePath(System.IO.Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true).Build();
        
     LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));

            var logger = NLogBuilder.ConfigureNLog(LogManager.Configuration).GetCurrentClassLogger();
            try
            {
                logger.Debug("init main");
                var host = CreateHostBuilder(args).Build();
                CreateDbIfNotExists(host);
                host.Run();
            }

我尝试在 seqlog 上打印 appsetting 中声明的属性,但这些属性没有打印。 我多次尝试更改语法但结果没有改变

I have an application in net core, the application writes all the logs in SeqLog, I used Nlog to configure everything and so far I have used an .xml file to give Nlog all the information useful for correct operation.
Browsing the Internet I saw that it is possible to use appsetting
instead of the .config file written in xml to declare all settings.
Now I would like to remove this .config file and write everything in appsetting, so I started writing everything and it seems to work except for one little thing:
this is what my .config file looks like:

<targets>
        <target name="seq" xsi:type="Seq" serverUrl="https://....." apiKey="....." >
            <property name="Application" value="Name Application" />
            <property name="MachineName" value="localhost" />
            <property name="Environment" value="TEST" />
            <property name="Body" value =" ${aspnet-request-posted-body}" />
            <property name="Source" value="${logger}" />
            <parameter name="@json" layout="${event-properties:item=JsonMessage}" />
            <parameter name="@error" layout="${event-properties:item=ErrorMessage}" />
        </target>
    </targets>
    <rules>
        <logger name="*" minlevel="Info" writeTo="seq" />
    </rules>

Then transferring everything into appsetting, this is how it looks:

"NLog": {
    "extensions": [
      { "assembly": "NLog.Targets.Seq" }
    ],
    "targets": {
      "seq": {
        "type": "Seq",
        "serverUrl": "https://.....",
        "apiKey": "........",
        "propertys": [
          {
            "name": "Application",
            "value": "Application_name"
          },
          {
            "name": "MachineName",
            "value": "localhost"
          },
          {
            "name": "Environment",
            "value": "TEST"
          },
          {
            "name": "Body",
            "value": "${aspnet-request-posted-body}"
          },
          {
            "name": "Source",
            "value": "${logger}"
          }
        ],
        "parameters": [
          {
            "name": "@json",
            "layout": "${event-properties:item=JsonMessage}"
          },
          {
            "name": "@error",
            "layout": "${event-properties:item=ErrorMessage}"
          }
        ]
      }   
  },
    "rules": [
      {
        "logger": "*",
        "minLevel": "Info",
        "writeTo": "seq"
      }
    ]
  },

The problem is that the properties are not read, so seqlog which should write me for example the Body or the name of the machine, does not, I have not found on the internet if the way in which I have declared the properties and the parameters is correct, but at this point I assume that it is not, so I wonder what is the correct way to declare properties and parameters in appsetting?

Could it also be a problem due to the configuration of the Program.cs?
Here is what I wrote:

    var config = new ConfigurationBuilder()
        .SetBasePath(System.IO.Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true).Build();
        
     LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));

            var logger = NLogBuilder.ConfigureNLog(LogManager.Configuration).GetCurrentClassLogger();
            try
            {
                logger.Debug("init main");
                var host = CreateHostBuilder(args).Build();
                CreateDbIfNotExists(host);
                host.Run();
            }

I tried to print the properties declared in appsetting on seqlog, but these are not printed.
I tried to change the syntax several times but the result didn't change

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

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

发布评论

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

评论(1

挽心 2025-01-24 11:14:39

作为帮助人们找到它的答案 - 但是Rolf Kristensen的评论看起来像是正确的答案。

这是 datalust/nlog-targgets-seq

    {
  "NLog": {
    "throwConfigExceptions": true,
    "targets": {
      "seq": {
        "type": "BufferingWrapper",
        "bufferSize": 200,
        "flushTimeout": 2000,
        "slidingTimeout": false,
        "target": {
          "type": "Seq",
          "serverUrl": "http://localhost:5341",
          "apiKey": "",
          "properties": [
          {
            "name": "Source",
            "value": "${Logger}",
          },
          {
            "name": "ThreadId",
            "value": "${ThreadId}",
            "as": "number"
          },
          {
            "name": "MachineName",
            "value": "${MachineName}",
          }]
        }
      }
    },
    "rules": [
    {
      "logger": "*",
      "minLevel": "Info",
      "writeTo": "seq"
    }]
  }
}

Posting as an answer to help people find it - but Rolf Kristensen's comment looks like the correct answer.

Here is the example from datalust/nlog-targets-seq

    {
  "NLog": {
    "throwConfigExceptions": true,
    "targets": {
      "seq": {
        "type": "BufferingWrapper",
        "bufferSize": 200,
        "flushTimeout": 2000,
        "slidingTimeout": false,
        "target": {
          "type": "Seq",
          "serverUrl": "http://localhost:5341",
          "apiKey": "",
          "properties": [
          {
            "name": "Source",
            "value": "${Logger}",
          },
          {
            "name": "ThreadId",
            "value": "${ThreadId}",
            "as": "number"
          },
          {
            "name": "MachineName",
            "value": "${MachineName}",
          }]
        }
      }
    },
    "rules": [
    {
      "logger": "*",
      "minLevel": "Info",
      "writeTo": "seq"
    }]
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文