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

Impossible to set up connection string programmatically. #53

Closed
DenKn opened this issue Oct 15, 2019 · 2 comments
Closed

Impossible to set up connection string programmatically. #53

DenKn opened this issue Oct 15, 2019 · 2 comments

Comments

@DenKn
Copy link

DenKn commented Oct 15, 2019

My config file

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Warn">
  <variable name="azureBlobConnString" value="" />

  <targets>

    <target name="azureLogs"
            xsi:type="AzureBlobStorage"
            layout="${layoutFormat}"
            blobName="myBlob"
            connectionString="${var:azureBlobConnString}"
            container="logs" />
 </targets>

My code
LogManager.Configuration.Variables["azureBlobConnString"] = "DefaultEndpointsProtocol=https;AccountName=*****;AccountKey=*****==;EndpointSuffix=core.windows.net";

But I get 'Connection string required' exception.

@snakefoot
Copy link
Contributor

snakefoot commented Oct 19, 2019

You need to setup the NLog variable before loading the NLog-config (ex creating NLog-Logger object).

Alternative you can do this:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Warn">
  <variable name="azureBlobDevConnString" value="UseDevelopmentStorage=true;" />
  <targets>

    <target name="azureLogs"
            xsi:type="AzureBlobStorage"
            layout="${layoutFormat}"
            blobName="myBlob"
            connectionString="${gdc:azureBlobConnString:whenEmpty=${var:azureBlobDevConnString}}"
            container="logs" />
  </targets>
</nlog>

And then do this:

NLog.GlobalDiagnosticsContext.Set("azureBlobConnString", "DefaultEndpointsProtocol=https;AccountName=*****;AccountKey=*****==;EndpointSuffix=core.windows.net");
LogManager.Configuration = LogManager.Configuration.Reload();  // Re-initialize blob-target

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

@DenKn
Copy link
Author

DenKn commented Nov 5, 2019

Yes. That works.

@DenKn DenKn closed this as completed Nov 5, 2019
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