gpt4 book ai didi

c# - 无法获取要跟踪的 ApplicationInsights

转载 作者:行者123 更新时间:2023-12-03 03:04:33 24 4
gpt4 key购买 nike

我创建了一个新的 Azure Web 作业、一个新的 Application Insights 资源,并且只是尝试让一个作业登录到另一个作业。据我了解,它应该像添加适当的 NuGet 包一样简单,并按照以下方式添加一些代码:

TelemetryClient tc = new TelemetryClient();
tc.InstrumentationKey = "my-key";
tc.TrackEvent("testing");
tc.TrackTrace("test diag");
tc.Flush();
System.Threading.Thread.Sleep(1000);

sleep 就在那里,因为我发现this MS 文章表明可能需要它。此代码在 Web 作业启动时发生(实际上,它只是一个控制台应用程序)。但是,当我运行它时,我没有得到任何指标。

我尝试将 key 放入 ApplicationInsights.config 中,但这没有什么区别。我还尝试了不同类型的日志记录,包括异常。

我的猜测是上面的代码没有按照我的想法进行,但如果有人能指出我正确的方向,说明原因,我将不胜感激。

最佳答案

我可以track user events通过在Azure Webjobs函数代码中插入TrackEvent调用,以下示例在我这边运行良好,您可以引用。

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.ApplicationInsights" version="2.4.0" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.4.1" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.4.1" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.WindowsServer" version="2.4.1" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.4.0" targetFramework="net461" />
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net461" />
<package id="Microsoft.Azure.WebJobs" version="2.0.0" targetFramework="net461" />
<package id="Microsoft.Azure.WebJobs.Core" version="2.0.0" targetFramework="net461" />
<package id="Microsoft.Azure.WebJobs.Extensions" version="2.0.0" targetFramework="net461" />
<package id="Microsoft.Data.Edm" version="5.7.0" targetFramework="net461" />
<package id="Microsoft.Data.OData" version="5.7.0" targetFramework="net461" />
<package id="Microsoft.Data.Services.Client" version="5.7.0" targetFramework="net461" />
<package id="Microsoft.Tpl.Dataflow" version="4.5.24" targetFramework="net461" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.2.1" targetFramework="net461" />
<package id="ncrontab" version="3.3.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
<package id="System.Diagnostics.DiagnosticSource" version="4.4.0" targetFramework="net461" />
<package id="System.Spatial" version="5.7.0" targetFramework="net461" />
<package id="WindowsAzure.Storage" version="7.2.1" targetFramework="net461" />
</packages>

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights;

namespace WebJob1
{
// To learn more about Microsoft Azure WebJobs SDK, please see https://go.microsoft.com/fwlink/?LinkID=320976
class Program
{
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
static void Main()
{
var config = new JobHostConfiguration();

TelemetryConfiguration.Active.InstrumentationKey = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

new TelemetryClient().TrackEvent("WebJobStart", new Dictionary<string, string> { { "appInsightsInstrumentationKey", TelemetryConfiguration.Active.InstrumentationKey } });

config.DashboardConnectionString = "";
config.UseTimers();


var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
}
}

函数.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;

namespace WebJob1
{
public class Functions
{
public static void TimerJob([TimerTrigger("00:01:00")] TimerInfo timerInfo, TextWriter log)
{
new TelemetryClient().TrackEvent("testing "+ DateTime.UtcNow.ToShortDateString(), new Dictionary<string, string> { { "appInsightsInstrumentationKey", TelemetryConfiguration.Active.InstrumentationKey } });

log.WriteLine("Process Something called at : " + DateTime.Now.ToShortDateString());
}
}
}

Azure 门户中的事件

enter image description here

关于c# - 无法获取要跟踪的 ApplicationInsights,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45811385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com