C# 仅限Application insight日志异常
我只想使用Application Insights记录异常。我该怎么做 我试着搜索关闭其他设置的方法,例如,它说没有办法关闭它 我尝试了ITelemetryProcessor,遇到了与此相同的问题。我尝试了配置和代码两种方法来注册ITelemetryProcessor,但即使我在Web API控制器中显式抛出异常,它也不会被命中 我正在使用VS 2017,并创建了一个新的.Net Framework 4.6.2 Web API。我还有一个InstrumentationKey,可以在Azure portal中看到记录的异常。首先,您引用的第一个与您的问题无关。 您只想记录异常,但这意味着要删除旧的遥测数据,如存储库中的Trace(在上传到app insights后存储遥测数据) 您只能使用ITelemetryProcessor记录异常。请按照以下步骤操作:C# 仅限Application insight日志异常,c#,azure,azure-application-insights,C#,Azure,Azure Application Insights,我只想使用Application Insights记录异常。我该怎么做 我试着搜索关闭其他设置的方法,例如,它说没有办法关闭它 我尝试了ITelemetryProcessor,遇到了与此相同的问题。我尝试了配置和代码两种方法来注册ITelemetryProcessor,但即使我在Web API控制器中显式抛出异常,它也不会被命中 我正在使用VS 2017,并创建了一个新的.Net Framework 4.6.2 Web API。我还有一个InstrumentationKey,可以在Azure p
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
namespace WebApplicationWebApi
{
public class ExceptionsFilter:ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
public ExceptionsFilter(ITelemetryProcessor next)
{
this.Next = next;
}
public void Process(ITelemetry item)
{
string s = item.GetType().Name;
//if it's not exception telemetry, just return without log it to app insights.
if (s != "ExceptionTelemetry")
{
return;
}
this.Next.Process(item);
}
}
}
1.右键单击项目名称->选择Configure Application insights
,将应用程序洞察添加到web api项目中:
添加SDK后,不要选择启用跟踪收集
:
2.在您的项目中添加一个.cs文件,然后实现您的自定义ITelemetryProcessor类,代码如下:
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
namespace WebApplicationWebApi
{
public class ExceptionsFilter:ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
public ExceptionsFilter(ITelemetryProcessor next)
{
this.Next = next;
}
public void Process(ITelemetry item)
{
string s = item.GetType().Name;
//if it's not exception telemetry, just return without log it to app insights.
if (s != "ExceptionTelemetry")
{
return;
}
this.Next.Process(item);
}
}
}
3.在ApplicationInsights.config中注册自定义ITelemetryProcessor。在节点中,添加
4.然后运行代码。要确保调用了自定义ITelemetryProcessor类,可以在该类中设置断点,以查看在运行时是否命中该类
出于测试目的,我在HomeController.cs中添加了一些遥测数据:
public class HomeController : Controller
{
TelemetryClient client = new TelemetryClient();
public ActionResult Index()
{
RequestTelemetry r1 = new RequestTelemetry();
r1.Name = "request message for testing";
client.TrackRequest(r1);
client.TrackTrace("trace message for testing wwwww.");
client.TrackException(new Exception("exception message for testing wwwww."));
ViewBag.Title = "Home Page";
return View();
}
}
5.在visual studio输出窗口中,您应该看到以下消息:
6.然后在visual studio中,导航到Application Insights Search
(在vs->view->other windows->Application Insights Search中),然后检查此处是否有一些值(如果在下面的屏幕截图中有类似“4”的值,请单击它):
7.如果步骤6中有值,请单击更新按钮
,然后选中全部
:
8.然后您可以看到只记录了异常:
嗨,简,你应该在这里发布你的代码。对于你的第一次引用,它意味着删除app insight中的旧数据,与你的问题无关。只是一个建议,为什么不使用log4net Hi Jan,下面的答案对你有用吗?@IvanYang让我稍后再试。我一核实就接受。谢谢你的回答。:)如果有任何问题,请让我知道:)嗨,我是azure新手,请告诉我ApplicationInsights.config在哪里。@AbhijeetChoudhari,ApplicationInsights.config仅适用于.net framework web项目。您使用的是.net framework web项目还是.net核心web项目,或者其他类似console项目的项目?