8

The ILogger interface has just one Log method:

void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter);

Application insights gives me a rich api like this:

TrackMetric(string name, double value, IDictionary<string, string> properties = null);
TrackException(Exception exception, IDictionary<string, string> properties = null, IDictionary<string, double> metrics = null);
TrackTrace(string message, SeverityLevel severityLevel, IDictionary<string, string> properties);

How should I implement a custom ILogger and still have all the features of application insights? One way I can think of is to define different TState types like below:

class Trace
{
    public string Message {get;set;};
    public IDictionary<string, string> Properties;

    public Trace(string message, IDictionary<string, string> properties)
    {
        this.Message = message;
        this.Properties = properties;
    }    
}

then when I need to trace, I do something like:

logger.Log<Trace>(LogLevel.Information, eventId, new Trace("sample trace",null));

In this way I switch which trackXXX method I used in the Log<TState> implementation based on the type of TSTate (I create types for Exception, Metric and Event). But this looks too complicated to write a simple trace, any other ways I am missing?

3
  • Question is - why do you need to use the ILogger interface at all? What's the use case? Commented Apr 18, 2017 at 6:23
  • The use case is that in dot net core its a first class citizen since it is injected automatically (if I implement the provider, factory, etc.) and would log internal dot net core traces by itself. Additionally I want to log to multiple places like event log, local file, and application insights, and that is easier (since there are existing providers for those purposes that I can add and just use, instead of writing all of those myself) Commented Apr 28, 2017 at 22:47
  • Hi @AzureMinotaur have you found a solution for this? Commented Mar 6, 2019 at 12:28

2 Answers 2

1

When you are using Application Insights SDK for ASP.NET Core and enable Application Insights, internal ILogger implementation is created. You can see here how it works.

So to answer your question, if you are using AI SDK for ASP.NET Core, you shouldn't need to implement your own ILogger. If you must however, you can use the linked implementation as example.

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

1 Comment

Good to know there is a simple default implementation for tracing only, but it does not allow me to track custom events or metrics. I wanted to use the ILogger interface which dotnet core supports "natively" to implement an App insights logger that can write custom events and metrics too (not just traces). Unfortunately the linked example does not give any guidance for that.
0

Here's the ILogger implementation for App Insights: https://github.com/Azure/azure-webjobs-sdk/blob/5140983cb163b20bf6af60afccebb705bc80ad80/src/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ApplicationInsightsLogger.cs

You could do roughly the same thing, wrapping the TelemetryClient, but adding support for events and other things like TrackDependency, which would get called from:

Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter)

based on the value of EventId.

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.