Skip to content

Commit 1e165fc

Browse files
committed
Added a new AddElmahIo method that will allow for configuration through settings. Also extended the .NET 8 sample with example code
1 parent 9b18b2b commit 1e165fc

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

samples/Elmah.Io.Blazor.Wasm.Example80/Elmah.Io.Blazor.Wasm.Example80/Elmah.Io.Blazor.Wasm.Example80.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@
99
<ItemGroup>
1010
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.5" />
1111
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.5" PrivateAssets="all" />
12+
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.0" />
1213
</ItemGroup>
1314

1415
<ItemGroup>
1516
<ProjectReference Include="..\..\..\src\Elmah.Io.Blazor.Wasm\Elmah.Io.Blazor.Wasm.csproj" />
1617
</ItemGroup>
1718

19+
<ItemGroup>
20+
<Content Update="wwwroot\appsettings.json">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</Content>
23+
</ItemGroup>
24+
1825
</Project>

samples/Elmah.Io.Blazor.Wasm.Example80/Elmah.Io.Blazor.Wasm.Example80/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#pragma warning disable S125 // Sections of code should not be commented out
12
using Elmah.Io.Blazor.Wasm;
23
using Elmah.Io.Blazor.Wasm.Example80;
34
using Microsoft.AspNetCore.Components.Web;
@@ -15,7 +16,6 @@
1516
o.ApiKey = "API_KEY";
1617
o.LogId = new Guid("LOG_ID");
1718

18-
#pragma warning disable S125 // Sections of code should not be commented out
1919
// Optional application name to set on all messages.
2020
//o.Application = "Blazor WASM 8.0 elmah.io sample";
2121

@@ -30,7 +30,11 @@
3030
//{
3131
// return msg.Detail != null && msg.Detail.Contains("Attempted");
3232
//};
33-
#pragma warning restore S125 // Sections of code should not be commented out
3433
});
3534

35+
// Elmah.Io.Blazor.Wasm can also be configured from appsettings.json like this:
36+
//builder.Services.Configure<ElmahIoBlazorOptions>(builder.Configuration.GetSection("ElmahIo"));
37+
//builder.Logging.AddElmahIo();
38+
3639
await builder.Build().RunAsync();
40+
#pragma warning restore S125 // Sections of code should not be commented out
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ElmahIo": {
3+
"ApiKey": "API_KEY",
4+
"LogId": "LOG_ID"
5+
}
6+
}

src/Elmah.Io.Blazor.Wasm/ElmahIoExtensions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ namespace Elmah.Io.Blazor.Wasm
1212
public static class LoggingBilderElmahIoExtensions
1313
{
1414
/// <summary>
15-
/// Add elmah.io with the specified options.
15+
/// Add Elmah.Io.Blazor.Wasm with the specified options.
1616
/// </summary>
1717
[System.Diagnostics.CodeAnalysis.SuppressMessage("Minor Code Smell", "S1075:URIs should not be hardcoded", Justification = "The URL will never be different")]
1818
public static ILoggingBuilder AddElmahIo(this ILoggingBuilder loggingBuilder, Action<ElmahIoBlazorOptions> configure)
1919
{
20+
loggingBuilder.AddElmahIo();
2021
loggingBuilder.Services.Configure(configure);
22+
return loggingBuilder;
23+
}
24+
25+
/// <summary>
26+
/// Add Elmah.Io.Blazor.Wasm without any options. Calling this method requires you to configure elmah.io options manually like this:
27+
/// <code>builder.Services.Configure&lt;ElmahIoBlazorOptions&gt;(builder.Configuration.GetSection("ElmahIo"));</code>
28+
/// Blazor WebAssembly doesn't have an appsettings.json file as a default. You will need to create one in the wwwroot directory.
29+
/// Make sure to set Build Action to Content and Copy to Output Directory to Copy if Newer.
30+
/// </summary>
31+
public static ILoggingBuilder AddElmahIo(this ILoggingBuilder loggingBuilder)
32+
{
2133
loggingBuilder.Services.AddSingleton<ILoggerProvider, ElmahIoLoggerProvider>(services =>
2234
{
2335
var httpClient = new HttpClient { BaseAddress = new Uri("https://api.elmah.io") };
@@ -26,5 +38,6 @@ public static ILoggingBuilder AddElmahIo(this ILoggingBuilder loggingBuilder, Ac
2638
});
2739
return loggingBuilder;
2840
}
41+
2942
}
3043
}

0 commit comments

Comments
 (0)