Skip to content
An experimental integration from Blazor to elmah.io.
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
sample/Elmah.Io.Blazor.Server
src/Elmah.Io.Blazor
.gitignore
Elmah.Io.Blazor.sln
README.md
appveyor.yml
global.json

README.md

Elmah.Io.Blazor

This is an experimental integration from Blazor to elmah.io. I wanted to see if I could get a WebAssembly-based application to log messages to elmah.io.

To start logging to elmah.io from Blazor, install the Elmah.Io.Blazor NuGet package:

Install-Package Elmah.Io.Blazor -IncludePrerelease

Then configure logging to elmah.io:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddLogging(l => l
        .AddElmahIo("API_KEY", new Guid("LOG_ID")));
    ...
}

Messages can be logged by injecting an ILogger into each page:

@page "/counter"
@using Microsoft.Extensions.Logging
@inject ILogger<Counter> logger

...

@functions {
    int currentCount = 0;

    void IncrementCount()
    {
        currentCount++;
        logger.LogInformation("Incremented count to {currentCount}", currentCount);
    }
}

In this example, I log an information message containing the new count.

Exceptions can be logged too:

@using Elmah.Io.Blazor.AspNetCore.Shared
@page "/fetchdata"
@using Microsoft.Extensions.Logging
@inject ILogger<Counter> logger
@inject HttpClient Http

...

@functions {
    WeatherForecast[] forecasts;

    protected override async Task OnInitAsync()
    {
        try
        {
            forecasts = await Http.GetJsonAsync<WeatherForecast[]>("api/SampleData/WeatherForecasts-nonexisting");
        }
        catch (Exception e)
        {
            logger.LogError(e, e.Message);
        }
    }
}

In this example, I log the exception happening, when requesting a non-existing json file from the server.

You can’t perform that action at this time.