Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Microsoft Application Insights with NLog (Target cannot be found: 'ApplicationInsights')

I am using Microsoft Application Insights for my Web Application. I used the Application Insights TraceListener NuGet package for logging. That worked perfectly.

Now I would like to switch to NLog. I added the Microsoft.ApplicationInsights.NLogTarget NuGet package and added a new NLog target in my NLog configuration file:

<target name='ai' xsi:type='ApplicationInsights' />

NLog throws an exception:

Target cannot be found: 'ApplicationInsights'

I also tried adding the assembly via extensions like so:

<extensions>
    <add assembly="Microsoft.ApplicationInsights.NLogTarget" />
</extensions>    

But it did not work either.

Any suggestions?

like image 468
Michael A. Volz aka Flynn Avatar asked Apr 29 '14 15:04

Michael A. Volz aka Flynn


People also ask

Where is ApplicationInsights config file?

By default, when you use the automated experience from the Visual Studio template projects that support Add > Application Insights Telemetry, the ApplicationInsights. config file is created in the project root folder. When it's compiled, it's copied to the bin folder.

How do I enable Azure application insights?

Select File | New Project. Select Visual C# | . NET Core | ASP.NET Core Web Application, ensure to mark the checkbox Add Application Insights to Project. Click Ok on the next screen.

How do I log exceptions in application insights?

Diagnose exceptions using Visual StudioOpen the Application Insights Search telemetry window in Visual Studio. While debugging, select the Application Insights dropdown box. Select an exception report to show its stack trace. To open the relevant code file, select a line reference in the stack trace.

How do I add application insights to an existing project in .NET core?

Open your project in Visual Studio. Go to Project > Add Application Insights Telemetry. Choose Azure Application Insights, then select Next. Choose your subscription and Application Insights instance (or create a new instance with Create new), then select Next.


1 Answers

If anyone else stumbles over this: The correct target type is ApplicationInsightsTarget not ApplicationInsights.

This works fine:

<extensions>
    <add assembly="Microsoft.ApplicationInsights.NLogTarget" />
</extensions>    
<targets>
    <target name='ai' xsi:type='ApplicationInsightsTarget' />
</targets>

No need to add the target by code.

See also: https://github.com/microsoft/ApplicationInsights-dotnet/tree/main/LOGGING#nlog

like image 143
mhaslhofer Avatar answered Sep 28 '22 05:09

mhaslhofer