serilog-sinks-azureblobstorage icon indicating copy to clipboard operation
serilog-sinks-azureblobstorage copied to clipboard

JSON configuration works in appsettings.json but not in Application settings.

Open YvonneArnoldus opened this issue 5 years ago • 4 comments

If I put the connection string in appsettings.json like this it works. "Serilog": { "WriteTo": [ {"Name": "AzureBlobStorage", "Args": {"connectionString": "", "storageContainerName": "", "storageFileName": ""}} ] } But if I follow good practice and put it in the applications settings of the web app in Azure like so:

Serilog:WriteTo:AzureBlobStorage.connectionString this conection string is not used.

I did the same with Serilog:WriteTo:ApplicationInsightsEvents.InstrumentationKey for "Serilog.Sinks.ApplicationInsights and that did work.

Any idea why it is not working here?

YvonneArnoldus avatar Feb 27 '19 10:02 YvonneArnoldus

Is this for a web application or some other type of application?

I haven't written code to test this, but I'm guessing this is because you haven't enabled environment settings for your configuration. The appsettings in Azure are maintained as environment variables.

For example, if you are building an IConfigurationBuilder, it should look like this:

IConfigurationBuilder builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: false)
                .AddJsonFile("appsettings.Development.json", optional: true)
                .AddEnvironmentVariables();

In web applications, you don't have to manually set up environment variable access, but if you're writing a console app or some other app, then you may have to enable this as shown above.

For more on configuration through settings, you may want to take a look at this link. This area is not really related to the BlobStorageSink, so if you continue to have problems you should post in that area.

chriswill avatar Mar 04 '19 23:03 chriswill

Hi,

I'm using .net core 2.2 which has a default method in program.cs that always includes the lines you wrote about.

And like I said I'm using both the Sink and Insights and the Insights information is in the environment variables and read. So turning on the reading of the environment variables is not the problem.

But the information for this Sink needs to be in the applications.json. Because if I put it in the environment variables (like: Serilog:WriteTo:AzureBlobStorage.connectionString) it is not read.

So or I named my environment variable wrong (Serilog:WriteTo:AzureBlobStorage.connectionString) or this sink does not use them.

YvonneArnoldus avatar Mar 05 '19 06:03 YvonneArnoldus

@YvonneArnoldus there's some similar discussion in this thread and it may be that adding the

"Using": [
      "Serilog.Sinks.AzureBlobStorage"
   ],

will help. I didn't have that in the Readme, but I've added it. I did a quick test and it didn't work for me, but perhaps this will help out your problem.

Personally, I use a pattern similar to my answer in the other thread. I store the Azure connection string as a setting in the Application Settings area. I then retrieve the value as the app starts and use the connection string to establish my Serilog configuration through csharp code in Program.cs.

Here's a short SO answer on how to access Configuration in Program.cs. When you run in an AppService, you can request AppService settings using the same code.

chriswill avatar Mar 11 '19 05:03 chriswill

@chriswill ,

Thank you for you links and explanations but as far as I can see I configured it right for an application setting in Azure.

I use the configuration approach, which means Serilog is only configured using application settings and no C# code accept reading of the configuration.

appsettings.json

"Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.ApplicationInsights",
      "Serilog.Sinks.AzureBlobStorage"
    ],
    "MinimumLevel": {
      "Default": "Error",
      "Override": {
        "Microsoft": "Error",
        "System": "Error",
        "Microsoft.AspNetCore.Authentication" :  "Error" 
      }
    },
    "WriteTo": [
      {
        "Name": "ApplicationInsightsEvents",
        "Args": {
          "InstrumentationKey": "override-in-appsettings"
        }
      },
      {
        "Name": "Console"
      },
{
        "Name": "AzureBlobStorage",
        "Args": {
          "connectionString": "override-in-appsettingsr",
          "storageContainerName": "myapp",
          "storageFileName": "{yyyy}/{MM}/{dd}/log.txt"
        }
      }
    ],
    "Properties": {
      "Application": "MyApp"
    }
  }

Where "override-in-appsettings" are overridden in Azure application settings with the correct values.

Program.cs

 public static void Main(string[] args)
    {
      try
      { 
        var webHost = BuildWebHost(args);
        webHost.Run();
      }
      finally
      {
        Log.CloseAndFlush();
        Thread.Sleep(2000); // give Serilog time to flush the messages
      }
    }

    public static IWebHost BuildWebHost(string[] args) =>
      WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .UseSerilog((hostingContext, loggerConfiguration) => {
          loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);
        })
        .Build();

I would like to keep this approach and not use a extra line to configure .UseSerilog because this means that I have to do if-else constructions to check if it needs to be turn on or create my on app settings segement to do turn it on or off.

It seems you configure your connections string for the blobstorage as a connection string in Azure. I have not tried that so maybe I'll try to register "Serilog:WriteTo:AzureBlobStorage.connectionString" in the Connection String segment of my Web App and see what happens then.

YvonneArnoldus avatar Mar 11 '19 07:03 YvonneArnoldus

Closing old issue

chriswill avatar Jul 17 '24 21:07 chriswill