Skip to content

Commit 2b43a7a

Browse files
committed
Enrich installation with environment variables and made an OnInstallation hook available in options
1 parent bcec612 commit 2b43a7a

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

samples/Elmah.Io.Extensions.Logging.AspNetCore90/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727

2828
// Remove comment on the following line to log through a proxy (in this case Fiddler).
2929
//options.WebProxy = new WebProxy("localhost", 8888);
30+
31+
// Enrich installation when notifying elmah.io after launch:
32+
//options.OnInstallation = installation =>
33+
//{
34+
// installation.Name = "ASP.NET Core 9.0 Application";
35+
// var logger = installation.Loggers.FirstOrDefault(l => l.Type == "Elmah.Io.Extensions.Logging");
36+
// logger?.Properties.Add(new Elmah.Io.Client.Item("Foo", "Bar"));
37+
//};
3038
});
3139

3240
// The elmah.io provider can log any log level, but we recommend only to log warning and up

samples/Elmah.Io.Extensions.Logging.Console90/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
//options.BatchPostingLimit = 20;
1717
//options.BackgroundQueueSize = 200;
1818
//options.Period = TimeSpan.FromSeconds(1);
19+
20+
// Enrich installation when notifying elmah.io after launch:
21+
//options.OnInstallation = installation =>
22+
//{
23+
// installation.Name = "ASP.NET Core 9.0 Application";
24+
// var logger = installation.Loggers.FirstOrDefault(l => l.Type == "Elmah.Io.Extensions.Logging");
25+
// logger?.Properties.Add(new Elmah.Io.Client.Item("Foo", "Bar"));
26+
//};
1927
}));
2028

2129
using (var serviceProvider = services.BuildServiceProvider())

src/Elmah.Io.Extensions.Logging/ElmahIoLoggerProvider.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Reflection;
45
using Elmah.Io.Client;
@@ -120,6 +121,7 @@ private void CreateInstallation()
120121
new AssemblyInfo { Name = "Elmah.Io.Client", Version = typeof(IElmahioAPI).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version },
121122
new AssemblyInfo { Name = "Microsoft.Extensions.Logging", Version = typeof(ILoggerProvider).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version }
122123
],
124+
EnvironmentVariables = [],
123125
};
124126

125127
var installation = new CreateInstallation
@@ -160,7 +162,14 @@ private void CreateInstallation()
160162
}
161163
}
162164

163-
api.Installations.Create(_options.LogId.ToString(), installation);
165+
if (EnvironmentVariablesHelper.TryGetElmahIoAppSettingsEnvironmentVariables(out List<Item> elmahIoVariables)) elmahIoVariables.ForEach(v => logger.EnvironmentVariables.Add(v));
166+
if (EnvironmentVariablesHelper.TryGetAspNetCoreEnvironmentVariables(out List<Item> aspNetCoreVariables)) aspNetCoreVariables.ForEach(v => logger.EnvironmentVariables.Add(v));
167+
if (EnvironmentVariablesHelper.TryGetDotNetEnvironmentVariables(out List<Item> dotNetVariables)) dotNetVariables.ForEach(v => logger.EnvironmentVariables.Add(v));
168+
if (EnvironmentVariablesHelper.TryGetAzureEnvironmentVariables(out List<Item> azureVariables)) azureVariables.ForEach(v => logger.EnvironmentVariables.Add(v));
169+
170+
_options.OnInstallation?.Invoke(installation);
171+
172+
api.Installations.CreateAndNotify(_options.LogId, installation);
164173
}
165174
catch
166175
{

src/Elmah.Io.Extensions.Logging/ElmahIoProviderOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public class ElmahIoProviderOptions
3030
/// </summary>
3131
public Action<CreateMessage> OnMessage { get; set; }
3232
/// <summary>
33+
/// Register an action to be called before creating an installation. Use the OnInstallation
34+
/// action to decorate installations with additional information related to your environment.
35+
/// </summary>
36+
public Action<CreateInstallation> OnInstallation { get; set; }
37+
/// <summary>
3338
/// Specify an action to be called on all (not filtered) messages if communication with the elmah.io API fails.
3439
/// </summary>
3540
public Action<CreateMessage, Exception> OnError { get; set; }

0 commit comments

Comments
 (0)