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

Azure Web App Settings not supported. #17

Open
YvonneArnoldus opened this issue Apr 17, 2019 · 6 comments
Open

Azure Web App Settings not supported. #17

YvonneArnoldus opened this issue Apr 17, 2019 · 6 comments

Comments

@YvonneArnoldus
Copy link

If I put the connection string for the eventhub in appsettings.json my logs are written to eventhub, but when I put the connections string in the App Settings of the Azure Web App no logs are written.

I'm using .Net Core 2.2 and all the latest version of the Serilog Packages.

appsettings.json

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.AzureEventHub"
    ],
    "MinimumLevel": {
      "Default": "Verbose",
      "Override": {
        "Microsoft": "Error",
        "System": "Error",
        "Microsoft.AspNetCore.Authentication": "Error"
      }
    },
    "WriteTo": [
      {
        "Name": "AzureEventHub",
        "Args": {
          "ConnectionString": "Endpoint=sb://*******.servicebus.windows.net/;SharedAccessKeyName=Send;SharedAccessKey=********",
          "EventHubName": "hubname",
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {SourceContext} - {Message}{NewLine}{Properties:j}{NewLine}{Exception}"
        }
      }
    ]
  }
}

Application settings

Serilog:WriteTo:AzureEventHub.ConnectionString Endpoint=sb://*******.servicebus.windows.net/;SharedAccessKeyName=Send;SharedAccessKey=********
Serilog:WriteTo:AzureEventHub.EventHubName hubname

Program.cs

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

Note: This is .Net Core 2.2 and the WebHost.CreateDefaultBuilder(args) adds:

config.AddJsonFile("appsettings.json", true, true).AddJsonFile("appsettings." + hostingEnvironment.EnvironmentName + ".json", true, true);
config.AddEnvironmentVariables();

I prefer not to commit the Event Hub ConnectionString in to my source code because that is bad practice.

@YvonneArnoldus
Copy link
Author

I found a work arround by reading the settings myself and then add them to the serilog configuration

Program.cs

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseApplicationInsights()
                .ConfigureLogging(logging => { logging.ClearProviders(); }) // clearing all other
                .UseSerilog(
                    (hostingContext, loggerConfiguration) =>
                    {

                        loggerConfiguration
                            .ReadFrom.Configuration(hostingContext.Configuration);

                        // Override EventHub Settings because Azure App Settings will not work with Serilog EventHub.
                        var eventHubConnectionString = hostingContext.Configuration.GetValue<string>("Serilog:WriteTo:AzureEventHub.ConnectionString");
                        var eventHubName = hostingContext.Configuration.GetValue<string>("Serilog:WriteTo:AzureEventHub.EventHubName");
                        if (!string.IsNullOrEmpty(eventHubConnectionString) && !string.IsNullOrEmpty(eventHubName))
                        {
                            loggerConfiguration.WriteTo.AzureEventHub(connectionString: eventHubConnectionString, eventHubName, outputTemplate: OutputTemplate);
                        }
                    });

No I don't have to include secrets in the appsettings.json but add them as application settings in my Azure Api App.

@sawilde
Copy link

sawilde commented Oct 7, 2019

@YvonneArnoldus there is a section here on dealing with arrays in appsettings when overriding from environment or keyvault

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/index?view=aspnetcore-2.2#bind-an-array-to-a-class
https://docs.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-2.2

have you tried the :0: (or --0-- ) syntax?

@yvonne-arnoldus
Copy link

@sawilde,

I'm not sure how dealing with arrays in appsettings is going to help me make the ReadFrom.Configuration form Serilog work with Azure Apps Settings. Unless Azure Apps Settings works different from appsettings.json.

At the moment it is not a problem for me any more because the work around I posted works fine for me, but it would be nice to also support Azure Apps Settings (not appsettings.json) for Event Hub Sink, because it does work for application insights

@axeh
Copy link

axeh commented Mar 3, 2020

I can confirm the same issue on .NET Core 3.1.
I can`t read overridden value in Azure Apps Settings

@berntrune
Copy link

Hi,
This syntax worked for me when binding to Seq

Serilog:WriteTo:1:Args:serverUrl

"Serilog": {
"Using": [ "Serilog.Sinks.Seq" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "D:\home\LogFiles\Log.txt",
"rollingInterval": "Day"
}
},
{
"Name": "Seq",
"Args": {
"ServerUrl": "http://localhost:5341"
}
}
]

@darena-patrick
Copy link

Any updates on how to get this to work from appsettings.json file? On .NET 5 trying something like this:

{  
  "Serilog": {
    "Using": ["Serilog.Sinks.AzureEventHub"],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "Enrich": ["FromLogContext","WithMachineName","WithProcessId","WithThreadId"],
    "WriteTo": [
      {
        "Name":"AzureEventHub",
        "Args": {
          "connectionString": "Endpoint=sb://...;SharedAccessKeyName=...;SharedAccessKey=...;EntityPath=...",
          "eventHubName": "..."
        }
      } 
    ]
  }
}

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

6 participants