Reputation: 4583
I was using TraceWriter class to write logs in my azure function. When function was small it was pretty well, I was able to see logs in function Monitor section and ApplicationInsight.
Now I'm building an application and want to use common logging across the application (Web, Jobs & functions) with DI.
I saw ILogger interface, It easily replaced with TraceWriter but for my other application (Web and Jobs), I want to resolve this using my own DI but I'm not able to find out an implemented class.
Serilog.TraceWriter looks good, but I don't want to use traceWriter in anywhere in my method parameters.
Any idea how to resolve ILogger or custom implementation? Which will work across the applications, I want to send my logs in ApplicationInsights and want to monitor using function monitor section as well
As of now I am using below custom code
public class Log : ILog
{
private static TelemetryClient telemetryClient = new TelemetryClient() { InstrumentationKey = ConfigurationManager.AppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] };
public void Error(string message, Exception ex = null)
{
telemetryClient.TrackTrace(message, SeverityLevel.Error);
if (ex != null)
telemetryClient.TrackException(ex);
}
public void Info(string message)
{
telemetryClient.TrackTrace(message, SeverityLevel.Information);
}
public void Verbose(string message)
{
telemetryClient.TrackTrace(message, SeverityLevel.Verbose);
}
public void Warning(string message)
{
telemetryClient.TrackTrace(message, SeverityLevel.Warning);
}
public TelemetryClient TelemetryClient
{
get
{
return telemetryClient;
}
}
}
Upvotes: 0
Views: 1008
Reputation:
I've been looking for a way to include the logging in services and I found that you can't use ILogger
and ILogger<T>
in both versions as ILogger<T>
will not show up in the logs of functions.
That's why I've created a solution with dependency injection which uses the ILogger
for functions and injects ILogger<T>
for the referenced services.
The advantage is that you don't need to change the code in the referenced services in order to make this work. And the included LoggingService and GenericLoggingService allow you to customize logging.
Please note that this currently is work in progress. I didn't test is on Azure yet.
Upvotes: 1