简体   繁体   English

应用洞察NLog目标

[英]Application insight NLog target

I have console application from which I want to send custom events to my Application Insight. 我有控制台应用程序,我想从我的Application Insight发送自定义事件。 I want to use Application Insight NLog target ( https://www.nuget.org/packages/Microsoft.ApplicationInsights.NLogTarget/ ) but it's not working. 我想使用Application Insight NLog目标( https://www.nuget.org/packages/Microsoft.ApplicationInsights.NLogTarget/ ),但它不起作用。 I tried to set it via .config file and tried to set it manually: 我试图通过.config文件设置它并尝试手动设置它:

    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? 有任何想法吗?

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. 然后,如果有任何问题从dc.services.visualstudio.com开箱即可,我会仔细检查Fiddler以及响应代码是什么。 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和其他诊断工具在本地对其进行故障排除

PerfView command to collect AI traces would look like: 用于收集AI跟踪的PerfView命令如下所示:

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

For my console application, I have read "INSTRUMENTATIONKEY" from App.config at run time. 对于我的控制台应用程序,我在运行时从App.config读取了“INSTRUMENTATIONKEY”。

So first I added "APPINSIGHTS_INSTRUMENTATIONKEY" as a key at App.config. 首先,我在App.config中添加了“APPINSIGHTS_INSTRUMENTATIONKEY”作为键。

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

Then read and set this key at Main function by adding below lines. 然后通过添加以下行来读取并在Main函数中设置此键。

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. 然后在您的Main函数或关闭函数的末尾添加一个thread.sleep,以便有时间将数据发送到Application Insights。

System.Threading.Thread.Sleep(70000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM