Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4844

Trying to use serilog-sinks-azureblobstorage in Blazor

$
0
0

Update:I have a very simple example here. Neither File or AzureBlobStorage work. In addition, if AzureBlobStorage is set, it gets an exception trying to load Blazor_Logger_bug.styles.css - which explains why I had pages not rendering when this was enabled.

I've switched to writing my own logger (wrote it up in less time than I spent trying to use Serilog). But wanted to warn others that it appears to not work and causes unrelated problems in your app. I've put my implementation up on GitHub and NuGet (way too much code to post here - 11 .cs files).


I am having all kinds of problems using this (Serilog to a file sink works fine) in a Blazor server app.

If I set up as follows:

var _logger = new LoggerConfiguration()    .ReadFrom.Configuration(config)    .CreateLogger();Log.Logger = _logger;builder.Logging.AddSerilog(Log.Logger);

then it logs to the Azure BLOB successfully. But I have the following problem:

  1. The application's page displays blank. The page source is _Host.cshtml without the two <component> nodes (which are everything).

If I set as follows:

builder.Logging.AddSerilog();

then the web page comes up fine. But I have the following problem:

  1. Nothing is logged to the Azure BLOB.

If I set it as follows:

builder.Host.UseSerilog((hostingContext, loggerConfiguration) =>{    loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);});

then the web page comes up fine.

But I have the following two problems:

  1. Nothing is logged to the Azure BLOB.
  2. The Console & Debug logging have nothing. I assume because this replaces the logging set to that point.

The full relevant code is:

if (builder.Environment.IsDevelopment()){    builder.Logging.ClearProviders();    builder.Logging.AddJsonConsole();    builder.Logging.AddDebug();}builder.Logging.AddSerilog();

And the relevant config is:

"Serilog": {"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.AzureBlobStorage" ],"MinimumLevel": {"Default": "Warning","Override": {"LouisHowe": "Information","ThirdPartyServices": "Information","CommonUtilities": "Information","Microsoft": "Warning"        }    },"WriteTo": [        {"Name": "AzureBlobStorage","Args": {"connectionString": "*****secret*****","storageContainerName": "logs","storageFileName": "{yyyy}-{MM}-{dd}.txt"            }        },        {"Name": "File","Args": {"path": "C:/temp/log-.txt","rollOnFileSizeLimit": true,"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter,Serilog.Formatting.Compact","rollingInterval": "Day"            }        }    ],"Enrich": [ "FromLogContext", "WithThreadId", "WithMachineName" ]},

How can I get this working?


Update: To repeat, I also have the Serilog File sink configured and it works fine.

I added Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg)); For the code:

builder.Host.UseSerilog((hostingContext, loggerConfiguration) =>{    loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration);});

or

var _logger = new LoggerConfiguration()    .ReadFrom.Configuration(config)    .CreateLogger();Log.Logger = _logger;builder.Logging.AddSerilog(Log.Logger);

I got the below in the output. No BLOB is created. The web page does not display.

2023-10-09T23:06:38.5791924Z Unable to find a method called WithThreadId. Candidate methods are:Serilog.LoggerConfiguration With(Serilog.Configuration.LoggerEnrichmentConfiguration, Serilog.Core.ILogEventEnricher)Serilog.LoggerConfiguration AtLevel(Serilog.Configuration.LoggerEnrichmentConfiguration, System.Action`1[Serilog.Configuration.LoggerEnrichmentConfiguration], Serilog.Events.LogEventLevel, Serilog.Core.LoggingLevelSwitch)Serilog.LoggerConfiguration FromLogContext(Serilog.Configuration.LoggerEnrichmentConfiguration)2023-10-09T23:06:38.5807420Z Unable to find a method called WithMachineName. Candidate methods are:Serilog.LoggerConfiguration With(Serilog.Configuration.LoggerEnrichmentConfiguration, Serilog.Core.ILogEventEnricher)Serilog.LoggerConfiguration AtLevel(Serilog.Configuration.LoggerEnrichmentConfiguration, System.Action`1[Serilog.Configuration.LoggerEnrichmentConfiguration], Serilog.Events.LogEventLevel, Serilog.Core.LoggingLevelSwitch)Serilog.LoggerConfiguration FromLogContext(Serilog.Configuration.LoggerEnrichmentConfiguration)

If I use builder.Logging.AddSerilog(); then there are no output messages with the word Serilog, no BLOB created, and the web site does come up.


Viewing all articles
Browse latest Browse all 4844

Trending Articles