我在我的应用程序中为Application Insights编写了一个自定义记录器。在Azure门户中查看App Insights时,我看不到任何异常或事件。这是记录器类代码,当我调试代码时,我确实看到一个分配给InstrumentationKey属性的键,你知道我做错了什么吗?是否需要将其他信息附加到客户端或配置?
public class AppInsightsLogger:ILogger
{
private TelemetryClient ai;
public AppInsightsLogger()
{
ai = new TelemetryClient();
if (string.IsNullOrEmpty(ai.InstrumentationKey))
{
// attempt to load instrumentation key from app settings
var appSettingsTiKey = AppSettings.InsightsKey;
if (!string.IsNullOrEmpty(appSettingsTiKey))
{
TelemetryConfiguration.Active.InstrumentationKey = appSettingsTiKey;
ai.InstrumentationKey = appSettingsTiKey;
}
else
{
throw new Exception("Could not find instrumentation key for Application Insights");
}
}
}
public void LogException(Exception ex)
{
ai.TrackException(ex);
}
}发布于 2016-01-29 02:05:35
我创建了一个新的控制台应用程序,安装了最新稳定的ApplicationInsights软件开发工具包,并且基本上保留了您的示例,但差别很小但很重要-我要么让它在调用TrackException后等待关闭,要么添加TelemetryClient.Flush()
namespace logtest
{
class Program
{
static void Main(string[] args)
{
AppInsightsLogger logger = new AppInsightsLogger();
logger.LogException(new InvalidOperationException("Is data showing?"));
// either wait for a couple of minutes for the batch to be sent of add ai.Flush() after ai.TrackException() to send the batch immediately
Console.ReadLine();
}
}
public class AppInsightsLogger
{
private TelemetryClient ai;
public AppInsightsLogger()
{
ai = new TelemetryClient();
if (string.IsNullOrEmpty(ai.InstrumentationKey))
{
// attempt to load instrumentation key from app settings
var appSettingsTiKey = "<ikey>";
if (!string.IsNullOrEmpty(appSettingsTiKey))
{
TelemetryConfiguration.Active.InstrumentationKey = appSettingsTiKey;
ai.InstrumentationKey = appSettingsTiKey;
}
else
{
throw new Exception("Could not find instrumentation key for Application Insights");
}
}
}
public void LogException(Exception ex)
{
ai.TrackException(ex);
// ai.Flush();
}
}
}首先,我可以在Visual Studio调试输出窗口中看到发送的遥测项:

然后我可以看到遥测在Fiddler中离开我的机器,我还可以看到它被我们的数据收集端点成功接受。

最后,我可以在门户中看到它:

发布于 2019-06-17 17:50:31
如今,这个问题和公认的答案已经过时了。目前的方法是添加Microsoft.ApplicationInsights.AspNet NuGet包,它具有开箱即用的日志记录功能。
文档中的要点:
通过default.
ApplicationInsightsLoggerProvider可以在appsettings.json中配置日志类型(如果确实需要,也可以通过编程方式进行配置) "Logging": {
"LogLevel": {
"Default": "Warning"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Error"
}
}
}发布于 2021-03-23 10:59:35
对于版本3的well作业,我尝试实例化TelemetryClient并调用TrackException(),以及调用Flush()并等待180秒,但这些都不起作用。起作用的是使用ILogger记录器对象并将异常传递给LogError()。
https://stackoverflow.com/questions/35048825
复制相似问题