Created
September 2, 2020 13:50
-
-
Save qbikez/cb394a0772404d426abb7c7ba232e8f7 to your computer and use it in GitHub Desktop.
Logging telemetry events via ILogger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
using Microsoft.ApplicationInsights; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.DependencyInjection.Extensions; | |
using Microsoft.Extensions.Logging; | |
namespace Telemetry | |
{ | |
// https://stackoverflow.com/questions/43455185/how-to-use-microsoft-extensions-logging-with-application-insights-to-log-events | |
// https://github.com/Azure/azure-webjobs-sdk/blob/5140983cb163b20bf6af60afccebb705bc80ad80/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ApplicationInsightsLogger.cs | |
public class TelemetryLogger : ILogger | |
{ | |
public static readonly EventId TrackEventId = new EventId(0, "TrackEvent"); | |
private readonly TelemetryClient telemetry; | |
public TelemetryLogger(TelemetryClient telemetry) | |
{ | |
this.telemetry = telemetry; | |
} | |
public IDisposable BeginScope<TState>(TState state) | |
{ | |
return new NoScope(); | |
} | |
public bool IsEnabled(LogLevel logLevel) => true; | |
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) | |
{ | |
if (eventId.Name == TrackEventId.Name) | |
{ | |
var stateDictionary = state as IReadOnlyList<KeyValuePair<string, object>>; | |
if (stateDictionary == null) | |
{ | |
return; | |
} | |
var properties = stateDictionary.ToDictionary(k => k.Key, k => Convert.ToString(k.Value, CultureInfo.InvariantCulture)); | |
var name = properties["_name"]; | |
telemetry.TrackEvent(name, properties); | |
} | |
} | |
class NoScope : IDisposable | |
{ | |
public void Dispose() | |
{ | |
} | |
} | |
} | |
public class TelemetryLoggerProvider : ILoggerProvider | |
{ | |
private readonly TelemetryClient telemetry; | |
public TelemetryLoggerProvider(TelemetryClient telemetry) | |
{ | |
this.telemetry = telemetry; | |
} | |
public ILogger CreateLogger(string categoryName) | |
{ | |
return new TelemetryLogger(telemetry); | |
} | |
public void Dispose() | |
{ | |
} | |
} | |
public static class LoggerExtensions | |
{ | |
public static void LogEvent(this ILogger logger, string name, Dictionary<string, string> properties = null) | |
{ | |
properties = properties ?? new Dictionary<string, string>(); | |
properties["_name"] = name; | |
object[] args = new object[properties.Count]; | |
var message = ""; | |
int i = 0; | |
foreach (var kvp in properties) | |
{ | |
message += $"{{{kvp.Key}}}"; | |
args[i++] = kvp.Value; | |
} | |
logger.Log(LogLevel.None, TelemetryLogger.TrackEventId, message, args); | |
} | |
} | |
public static class ServicesExtension | |
{ | |
public static void AddTelemetryLogger(this IServiceCollection services) | |
{ | |
services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, TelemetryLoggerProvider>()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment