Home > Software design >  Configure in the appsetting Nlog to write in Seq
Configure in the appsetting Nlog to write in Seq

Time:04-01

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

CodePudding user response:

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",
            "layout": "${Logger}",
          },
          {
            "name": "ThreadId",
            "layout": "${ThreadId}",
            "as": "number"
          },
          {
            "name": "MachineName",
            "layout": "${MachineName}",
          }]
        }
      }
    },
    "rules": [
    {
      "logger": "*",
      "minLevel": "Info",
      "writeTo": "seq"
    }]
  }
}
  • Related