Search code examples
loggingazure-application-insightspostsharplog4postsharp

How do I use postsharp logging to write to applicationInsights? and How do I test that the logs are being written to application Insights?


I was trying to use application Insights as my logging backend from postsharp references. I have setup the application insights and was able to see live metrics. But I am not quite sure if postsharp logging alone is able to write to application insights.

My code in Program.cs

using PostSharp.Patterns.Diagnostics;

using PostSharp.Patterns.Diagnostics.Backends.ApplicationInsights;

LoggingServices.DefaultBackend = new ApplicationInsightsLoggingBackend("InstrumentationKey");

GlobalAspects.cs file:

using PostSharp.Patterns.Diagnostics;

using PostSharp.Extensibility;

// This file contains registration of aspects that are applied to several classes of this project.

[assembly: Log(AttributeTargetTypeAttributes=MulticastAttributes.Public, AttributeTargetMemberAttributes=MulticastAttributes.Public)]

Do I need to setup anyhting in my program.cs in order to completely make use of postsharp logging to write to applicationinsights.


Solution

  • You need to set the default verbosity of the Application Insights logging backend to Trace to get the method call log records.

    LoggingServices.DefaultBackend.DefaultVerbosity.SetMinimalLevel(LogLevel.Trace);