4

How to implement logging in blazor

I’m using Blazor (3.1) Server approach

I want to log (to a file) some events

I have try this extension: https://github.com/BlazorExtensions/Logging but I can’t make it to work as it says.

Can someone point me to a working example? Or Tell me if I’m doing something wrong (I’m getting this error with this approach:

A circular dependency was detected for the service of type 'Microsoft.JSInterop.IJSRuntime)

3 Answers 3

4

In order to implement logging for Blazor server you would follow the same approach as you would for a .NET Core or ASP.NET Core application.

Namely in your Program.cs file you would need to modify the CreateHostBuilder method to configure your loggers in a manner such as

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.AddConsole();
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

You could then inject an ILogger into your razor components or throughout the rest of your application using dependency injection.

public class AboutModel : PageModel
{
    private readonly ILogger _logger;

    public AboutModel(ILogger<AboutModel> logger)
    {
        _logger = logger;
    }
    public string Message { get; set; }

    public void OnGet()
    {
        Message = $"About page visited at {DateTime.UtcNow.ToLongTimeString()}";
        _logger.LogInformation(Message);
    }
}

Be sure to check the Microsoft documentation for information on built in loggers, third party loggers, and just logging in general.

Sign up to request clarification or add additional context in comments.

3 Comments

in Blazor Server .net5 APP I do not know why ErrorModel class logs my LogError and another log message. How can I disable the default logger
On my IIS server, the log information is not save to a .log file in %SystemDrive%\inetpub\logs\LogFiles as I would expect, how do I save to the log file? Thanks
@LukeTO'Brien That is beyond the scope of a comment here, it would be best to ask a new question and provide clarifying details to your issues along with relevant parts of your code.
3

you can use Serilog

this implementation for server side https://www.c-sharpcorner.com/article/log-data-into-file-using-serilog-framework-in-blazor-server-app/

and this for client side https://nblumhardt.com/2019/11/serilog-blazor/

also you will need to read https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0

Comments

2

Right now, it appears that logging with Blazor.Extensions.Logging is not supported for Blazor Server-Side applications due to the circular dependency with IJSRuntime:

https://github.com/BlazorExtensions/Logging/issues/44

PHeuter: "IJSRuntime may need to log and the logger needs IJSRuntime to do the logging."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.