NLog use Connection String Name in appsettings
Asked Answered
T

2

11

I have an NLog database target that looks like this:

<target xsi:type="Database" name="database"
      connectionString="Server=.\SQLEXPRESS;Database=ApplicationOne;Trusted_Connection=True;MultipleActiveResultSets=true;User Id=User0101;Password=PW0101"   
      commandText="INSERT INTO [SchemaOne].[EventLogs](Id, Message, Level, Logger )VALUES(NewID(), @Message, @Level, @Logger)">
  <parameter name="@Message" layout="${message}" />
  <parameter name="@Level" layout="${level}" />
  <parameter name="@Logger" layout="${logger}" />
</target>

Is it possible to change the connectionString to use connectionStringName from my appsettings instead?

My appsettings is called dssettings.json and it contains the connection details here:

"DatabaseConfiguration": {
    "DatabaseName": "ApplicationOne",
    "ConnectionName": "DefaultConnection",
    "ConnectionString": "Server=.\\SQLEXPRESS;Database=ApplicationOne;Trusted_Connection=True;MultipleActiveResultSets=true;User Id=User0101;Password=PW0101" 
  },
Testudo answered 30/12, 2018 at 11:55 Comment(3)
You shouldn't store the connection string in neither one, its not secure and you could accidentally commit it into your source control management system (Git, CSV etc.). Use user secrets, environment variables or azure key value storeBergquist
I understand but this is for a small project that has been using appsettings for awhile now.Testudo
You could consider using the NLog Configuration API.Escalate
Z
10

Update NLog.Extension.Logging ver. 1.4.0

With NLog.Extension.Logging ver. 1.4.0 then you can now use ${configsetting}

See also: https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer

Original Answer

With help from nuget-package NLog.Appsettings.Standard then you can normally do this:

  <extensions>
    <add assembly="NLog.Appsettings.Standard" />
  </extensions>
  <targets>
    <target xsi:type="Database" name="database"
          connectionString="${appsettings:name=DatabaseConfiguration.ConnectionString}"   
          commandText="INSERT INTO [SchemaOne].[EventLogs](Id, Message, Level, Logger )VALUES(NewID(), @Message, @Level, @Logger)">
      <parameter name="@Message" layout="${message}" />
      <parameter name="@Level" layout="${level}" />
      <parameter name="@Logger" layout="${logger}" />
    </target>
  </targets>

But because you are using a special dssettings.json (instead of appsettings.json), then you probably have to implement your own custom NLog layout renderer:

https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer

Maybe you can use the source-code from the above nuget-package as inspiration for loading dssettings.json. Or maybe create PullRequest that adds support for specifying non-default config-filename.

Zanazander answered 30/12, 2018 at 16:9 Comment(2)
Hello, thank you for replying. Is it possible to have 2 appsettings.json then? So one dssettings.json and one appsettings.jsonTestudo
The ConfigurationBuilder.AddJsonFile can load from any filename. But like I said you have to implement the logic yourself. Maybe offer it as pull-request that allows one to provide custom filename.Zanazander
P
0

1- Install NLog.Extensions.Logging from nuget

2- Change connectionString like this

<target xsi:type="Database" name="database"
      connectionString="${configsetting:item=DatabaseConfiguration.ConnectionString}"   
      commandText="INSERT INTO [SchemaOne].[EventLogs](Id, Message, Level, Logger )VALUES(NewID(), @Message, @Level, @Logger)">
  <parameter name="@Message" layout="${message}" />
  <parameter name="@Level" layout="${level}" />
  <parameter name="@Logger" layout="${logger}" />
</target>
Prayerful answered 3/4 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.