Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set connection string for Azure Data Tables using ${var:connectionString} #135

Open
akakira opened this issue Feb 21, 2023 · 3 comments

Comments

@akakira
Copy link

akakira commented Feb 21, 2023

Recently I've been converting out sql server logging using nlog to azure data tables, using your AzureStorage extension. Everything works great when I set the connection string via text in the nlog config file, or via the appsettings. However due to security restrictions I must load the connection string from a keyvault on startup. I have modified my nlog.config like so:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogLevel="Trace" internalLogToConsole="true">
  <extensions>
    <add assembly="NLog.Extensions.AzureDataTables" /> 
  </extensions>
  <targets>
    <target xsi:type="AzureDataTables"
            connectionString="${var:storageConnectionString}"
            name="AzureTable"
            tableName="ErrorLogs">
      <contextproperty name="CallSite" layout="${callsite:filename=true}" />
      <contextproperty name="Date" layout="${longdate}" />
      <contextproperty name="Exception" layout="${exception}" />
      <contextproperty name="Level" layout="${level}" />
      <contextproperty name="Logger" layout="${logger}" />
      <contextproperty name="MachineName" layout="${machinename}" />
      <contextproperty name="Message" layout="${message}" />
      <contextproperty name="StackTrace" layout="${stacktrace}" />
      <contextproperty name="Thread" layout="${threadid}" />
    </target>
  </targets>
  <rules>
    <logger name="*" writeTo="AzureTable"  />
  </rules>
</nlog>

and set that during my Startup.cs using the following:
LogManager.Configuration.Variables["storageConnectionString"] = Settings.StorageConnectionString;

The connection string never gets set using that method. Is there a workaround for this?

@snakefoot
Copy link
Contributor

You can perform an explict reload after having assigned the wanted ConnectionString:

NLog.LogManager.Setup().LoadConfiguration(builder => {
    builder.Configuration.Variables["storageConnectionString"] = Settings.StorageConnectionString;
}).ReloadConfiguration();

If still using NLog v4, then you can do it like this:

NLog.LogManager.Configuration.Variables["storageConnectionString"] = Settings.StorageConnectionString;
NLog.LogManager.Configuration = NLog.LogManager.Configuration.Reload();

See also: https://github.com/NLog/NLog/wiki/Reinitialize-NLog-configuration

@akakira
Copy link
Author

akakira commented Feb 23, 2023

You can perform an explict reload after having assigned the wanted ConnectionString:

NLog.LogManager.Setup().LoadConfiguration(builder => {
    builder.Configuration.Variables["storageConnectionString"] = Settings.StorageConnectionString;
}).ReloadConfiguration();

If still using NLog v4, then you can do it like this:

NLog.LogManager.Configuration.Variables["storageConnectionString"] = Settings.StorageConnectionString;
NLog.LogManager.Configuration = NLog.LogManager.Configuration.Reload();

See also: https://github.com/NLog/NLog/wiki/Reinitialize-NLog-configuration

Thanks man, that works, but surely there's a simpler way to get the config from a variable than this.

@snakefoot
Copy link
Contributor

Thanks man, that works, but surely there's a simpler way to get the config from a variable than this.

That would require changing AzureDataTables-target in this repository to handle dynamic ConnectionString-Layout-values.

The NLog DatabaseTarget depends on using SqlConnection-pool, but there is no Azure-DataTables-Connection-Pool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants