1
votes

I have a ASP.NET Core 2.x Azure web app. I'm using Serilog to handle the logging. I would like my log files to end up in Azure Blob Storage but have been unable to get it configured properly.

As per the instructions at this link, I have enabled "Blob Application Logging" with minimum level "Information".

In my web app's Startup.cs file, I have configured Serilog as follows:

Serilog.Log = new LoggerConfiguration()               
               .WriteTo.Trace(Serilog.Events.LogEventLevel.Information)
               .WriteTo.Console(Serilog.Events.LogEventLevel.Information)
            .WriteTo.RollingFile(@"../logs/logfile-{Date}.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level}:{EventId} [{SourceContext}] {Message}{NewLine}{Exception}")
            .CreateLogger();

With this configuration, the local file system rolling file sink seems to work just fine, but I'm not getting any logs from Serilog in the blob storage files.

I do get SOME logs in the blob storage logs, but none of them are from Serilog, they all seem to come from the .NET framework itself.

Am I missing some kind of link that would allow me to pipe Serilog output to the blob storage logs along with the Microsoft logs?

3

3 Answers

4
votes

The Serilog.Sinks.AzureApp sink is designed for this - it uses the settings in the "diagnostic logs" section of the Azure portal. See its github page for more details, and also the discussion here.

0
votes

To write logs to Azure Storage, we need to install the package of Serilog.Sinks.AzureTableStorage. We can install it via NuGet as below:

enter image description here

And we can do in our code using method WriteTo.AzureTableStorage, which looks like below:

enter image description here

0
votes

For Serilog, there is no sinks for azure blob from it's offical site here.

I find a third-party sink for azure blob named Mike.Serilog.Sinks.AzureStorage, which can write Serilog log messages to a specifid blob(it's a different blob from the blob stored .net framework log), here.

If you like to have a try, please follow the steps below:

step 1: Download the project from here .

step 2: Open the RollingAzureBlobSink.cs -> in line 62, use this line of code await _blob.AppendTextAsync(evnt.MessageTemplate.Text); to replace the original code. Then build the project. enter image description here

step 3: Create a new ASP.NET Core 2.0 project, and add the reference "Mike.Serilog.Sinks.AzureStorage.dll" in step 2.

step 4: Add the following code in the Starup.cs, method public Startup(IConfiguration configuration) as following:

public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            StorageCredentials credentials = new StorageCredentials("your storage account", "your key");
            CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, false);

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Trace(Serilog.Events.LogEventLevel.Information)
                .WriteTo.Console(Serilog.Events.LogEventLevel.Debug)
                .WriteTo.RollingFile(@"your file path", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level}:{EventId} [{SourceContext}] {Message}{NewLine}{Exception}")
                .WriteTo.RollingAzureBlobSink(null, storageAccount, "testyy", "jj", 5, TimeSpan.FromMinutes(3))
                .CreateLogger();

            Log.Debug("a good thing debug");
            Log.Information("a info inforxxxxx");
        }

step 5: Publish to azure, and remember to enable "Blob Application Logging" with minimum level "Information".

step 6: Run the website, and you can see in your storageAccouont -> container(here is testyy), a log starts with jj are created, and Serilog are written into it. enter image description here