user2086174
user2086174

Reputation: 195

Application insight NLog target

I have console application from which I want to send custom events to my Application Insight. I want to use Application Insight NLog target (https://www.nuget.org/packages/Microsoft.ApplicationInsights.NLogTarget/) but it's not working. I tried to set it via .config file and tried to set it manually:

    var config = new LoggingConfiguration();
    ConfigurationItemFactory.Default.Targets.RegisterDefinition("ai", typeof(ApplicationInsightsTarget));
    ApplicationInsightsTarget aiTarget = new ApplicationInsightsTarget();
    aiTarget.InstrumentationKey = "my_key";
    aiTarget.Name = "aiTarget";
    LoggingRule rule = new LoggingRule("*", LogLevel.Info, aiTarget);
    config.AddTarget("aiTarget", aiTarget);
    config.LoggingRules.Add(rule);
    LogManager.Configuration = config;

but still nothing, I can't see my exceptions or events in application insights. Any ideas?

Upvotes: 1

Views: 5107

Answers (2)

Rousonur Jaman
Rousonur Jaman

Reputation: 1261

For my console application, I have read "INSTRUMENTATIONKEY" from App.config at run time.

So first I added "APPINSIGHTS_INSTRUMENTATIONKEY" as a key at App.config.

<appSettings>
    ....
    <add key="APPINSIGHTS_INSTRUMENTATIONKEY" value="your key" />
    ....
</appSettings>

Then read and set this key at Main function by adding below lines.

var key = ConfigurationManager.AppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"];   
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = key;

Then at the end of you Main function or in your closing function add a thread.sleep for giving some time to send data to Application Insights.

System.Threading.Thread.Sleep(70000);

Upvotes: 0

Dmitry Matveev
Dmitry Matveev

Reputation: 2679

I assume you followed documentation here (which is pretty close to what you implemented):

var config = new LoggingConfiguration();

ApplicationInsightsTarget target = new ApplicationInsightsTarget();
// You need this only if you did not define InstrumentationKey in ApplicationInsights.config or want to use different instrumentation key
target.InstrumentationKey = "Your_Resource_Key";

LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target);
config.LoggingRules.Add(rule);

LogManager.Configuration = config;

Logger logger = LogManager.GetLogger("Example");

logger.Trace("trace log message");

Then, I'd double check with Fiddler if anything gets sent out of the box to dc.services.visualstudio.com and what's the response code. That might give a clue about the issue if the problem is indeed with the transport but not collection.

If the issue is in collection, you can troubleshoot it locally with PerfView and other Diagnostics tools.

PerfView command to collect AI traces would look like:

PerfView.exe /onlyProviders=*Microsoft-ApplicationInsights-Extensibility-Web,*Microsoft-ApplicationInsights-Web,*Microsoft-ApplicationInsights-Core,*Microsoft-ApplicationInsights-Extensibility-DependencyCollector,*Microsoft-ApplicationInsights-Extensibility-Rtia-SharedCore,*Microsoft-ApplicationInsights-Extensibility-WindowsServer,*Microsoft-ApplicationInsights-WindowsServer-TelemetryChannel collect

Upvotes: 2

Related Questions