據我所知,TelemetryClient.TrackException可以添加自定義屬性。
添加這些屬性之後,你會發現在蔚藍的見解門戶值。
更多有關如何在網頁API添加自定義屬性的詳細信息,你可以參考下面的代碼:
:
public class AiExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
if (context != null && context.Exception != null)
{//or reuse instance (recommended!). see note above
var ai = new TelemetryClient();
//Here you need get the CustomerId, JobId, and other. For example get the current user id
string re = HttpContext.Current.User.Identity.Name;
// Set up some properties:
//Here you need get what you need and add them to the properties
var properties = new Dictionary<string, string> {{ "Users", "vvvvv" } };
// Send the exception telemetry:
ai.TrackException(context.Exception, properties);
}
base.Log(context);
}
}
你可以在見下面的所有屬性中找到它
但我有其他的問題,我該怎麼做才能從我的控制器「AiExceptio發送數據nLogger」。即:我有我的控制器POST方法Post(user,jobId),我想添加jobId TrackException。注意:我不想爲我的控制器中的每個方法使用try {} catch(){},如果我可以將該信息添加到上下文中,它將是grat !.謝謝!
根據您的描述,我建議您可以嘗試另一種方式註冊過濾器並重寫OnActionExecuted方法。
在這種方法中,你可以先檢查異常爲空。如果異常不爲null,則可以從HttpActionExecutedContext獲取ActionArguments。
然後,你可以在屬性中添加此參數,並將其發送到Azure應用程序見解。
更多細節,你可以參考下面的代碼:
WebApiConfig:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//config.Services.Add(typeof(IExceptionLogger), new AiExceptionLogger());
config.Filters.Add(new AAA());
}
public class AAA : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception != null)
{
var ai = new TelemetryClient();
//here get the arguments
string d1 = (string)actionExecutedContext.ActionContext.ActionArguments["id"];
var properties = new Dictionary<string, string> { { "Users", d1 } };
ai.TrackException(actionExecutedContext.Exception, properties);
}
base.OnActionExecuted(actionExecutedContext);
}
}
結果:
閱讀有關'TelemetryInitializers',看到的是https://文檔.microsoft.com/EN-US /天藍色/應用程序的見解/ APP-見解-API過濾採樣#附加屬性 - itelemetryinitializer。您可以將自定義屬性添加到所有項目中,例如請求和異常等等。儘管如此,它仍由您來注入適當的值。你可以創建一個工廠類來完成JobId等工作。這取決於你如何訪問這些信息。 –