应用洞察NLog目标

时间:2016-08-08 12:51:02

标签: c# nlog azure-application-insights

我有控制台应用程序,我希望将自定义事件发送到我的Application Insight。我想使用Application Insight NLog目标(https://www.nuget.org/packages/Microsoft.ApplicationInsights.NLogTarget/),但它不起作用。我试图通过.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;

但仍然没有,我无法在应用程序洞察中看到我的异常或事件。有任何想法吗?

2 个答案:

答案 0 :(得分:0)

我假设您遵循了文档here(这与您实施的内容非常接近):

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");

然后,如果有任何问题从dc.services.visualstudio.com发出,我会仔细检查Fiddler以及响应代码是什么。如果问题确实存在于传输而非收集,那么这可能会给出问题的线索。

如果问题在收集中,您可以使用PerfView and other Diagnostics tools.

在本地对其进行问题排查

用于收集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

答案 1 :(得分:0)

对于我的控制台应用程序,我已阅读" INSTRUMENTATIONKEY"从运行时的App.config开始。

首先我添加了" APPINSIGHTS_INSTRUMENTATIONKEY"作为App.config的一个关键。

Sub RunQueriesInAccess()

    Dim AC As Object
    Set AC = CreateObject("Access.Application")
    Dim strDatabasePath As String
    strDatabasePath = ThisWorkbook.Path & "\Database1.accdb"

    With AC
        .OpenCurrentDatabase (strDatabasePath)
        Dim db As Object
        Set db = .CurrentDb
        .DoCmd.SetWarnings False
        .DoCmd.OpenQuery "qry_RISK_RATING"
        .DoCmd.OpenQuery "qry_Delete_ALLL"
        Dim qry As Object
        Set qry = db.QueryDefs("qry_DATA_HIST")
        qry.Parameters(0) = Worksheets("Impact Analysis New").Range("B1").Value
        qry.Execute

        Set qry = db.QueryDefs("qry_LIMIT_HIST")
        qry.Parameters(0) = Worksheets("Impact Analysis New").Range("B1").Value
        qry.Execute
        .DoCmd.SetWarnings True
        .Quit
    End With
    ActiveWorkbook.RefreshAll
End Sub

然后通过添加以下行来读取并在Main函数中设置此键。

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

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

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