2017-09-04 37 views
0

我已配置了在服務器異常時觸發的Application Insights警報。 我正在處理這個警報通過一個邏輯應用程序,它工作正常。在應用程序洞察中獲取異常詳細信息webhook JSON

我想編輯警報負載JSON,以便它包含其中的異常詳細信息。 從上設置警報可用documentation,我們似乎可以編輯的屬性,包括一些自定義的數據,可以使用Azure的監視器的API

Azure的監視器API Alert Rules Documentation並沒有過多提及這個設置。

有什麼建議嗎?

+0

你的意思是你想從異常中得到詳細的錯誤信息並將這些信息發送給AI?據我所知,輸出會得到錯誤信息,你可以使用邏輯應用程序調用azure函數,在azure函數中發送這些錯誤信息給AI。關於如何在邏輯應用中調用azure函數,可以參考本文[文章](https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-scenario-function-sb-觸發)。 –

+0

@BrandoZhang,是的,我可以通過一個邏輯應用程序得到這個工作。謝謝。 – ak92

回答

0

如果您需要發送到Application Insights的異常信息,請嘗試這些類。

public static class StaticLogger 
{ 
    private static Object theThreadLock = new Object(); 

    private static TelemetryClient _APM; 
    public static TelemetryClient APM 
    { 
     get 
     { 
      lock (theThreadLock) 
      { 
       if (_APM == null) { _APM = new TelemetryClient(); } 
       return _APM; 
      } 
     } 
    } 

    public static void Warning(string message) 
    {    
     APM.TrackTrace(message, SeverityLevel.Warning); 
    }     

    public static void Error(Exception exception,LoggerEvents logEvent) 
    { 
     lock (theThreadLock) 
     { 
      var telemetry = new ExceptionTelemetry(exception); 
      APM.TrackEvent(logEvent.ToString()); 

      APM.TrackException(telemetry); 
     } 
    } 

    public static void Error(Exception exception,LoggerEvents logEvent, string fmt, params object[] vars) 
    { 
     lock (theThreadLock) 
     { 
      var telemetry = new ExceptionTelemetry(exception); 
      telemetry.Properties.Add("message", string.Format(fmt, vars)); 

      APM.TrackException(telemetry); 
     } 
    } 

    public static void Trace(string Message, string userID) 
    { 
     lock(theThreadLock) 
     { 
      APM.Context.User.Id = userID.ToString(); 
      Trace(Message); 
     } 

    } 
    public static void Trace(string Message) 
    { 
     lock (theThreadLock) 
     { 
      APM.TrackTrace(Message); 
     } 
    } 

    public static void TraceAPI(string currentNameSpace,string componentName, string method, string httpAction, TimeSpan timespan) 
    { 
     TraceAPI(currentNameSpace,componentName, method, httpAction, timespan, string.Empty); 
    } 

    public static void TraceAPI(string currentNameSpace,string componentName, string method, string httpAction, TimeSpan timespan, string fmt, params object[] vars) 
    { 
     TraceAPI(currentNameSpace,componentName, method, httpAction, timespan, string.Format(fmt, vars)); 
    } 

    public static void TraceAPI(string currentNameSpace,string componentName, string method, string httpAction, TimeSpan timespan, string properties) 
    { 
     lock (theThreadLock) 
     { 
      var telemetry = new TraceTelemetry("Trace API call", SeverityLevel.Information); 
      telemetry.Properties.Add("namespace", currentNameSpace); 
      telemetry.Properties.Add("component", componentName); 
      telemetry.Properties.Add("method", method); 
      telemetry.Properties.Add("HttpAction", httpAction); 
      telemetry.Properties.Add("timespan", timespan.ToString()); 

      if (!string.IsNullOrWhiteSpace(properties)) 
       telemetry.Properties.Add("properties", properties); 

      APM.TrackTrace(telemetry); 
     } 
    } 

    public static void TraceAPI(string currentNameSpace, string componentName, string method, string httpAction, TimeSpan timespan, IDictionary<string, string> properties = null) 
    { 
     lock (theThreadLock) 
     { 
      var telemetry = new TraceTelemetry("Trace API call", SeverityLevel.Information); 
      telemetry.Properties.Add("namespace", currentNameSpace); 
      telemetry.Properties.Add("component", componentName); 
      telemetry.Properties.Add("method", method); 
      telemetry.Properties.Add("HttpAction", httpAction); 
      telemetry.Properties.Add("timespan", timespan.ToString()); 

      if (properties != null) 
      { 
       foreach (var property in properties) 
       { 
        telemetry.Properties.Add(property.Key, property.Value); 
       } 
      } 

      APM.TrackTrace(telemetry); 
     } 
    } 

    public static void TraceMethod(string currentNameSpace, string componentName, string method, TimeSpan timespan, string properties) 
    { 
     lock (theThreadLock) 
     { 
      var telemetry = new TraceTelemetry("Trace API call", SeverityLevel.Information); 
      telemetry.Properties.Add("namespace", currentNameSpace); 
      telemetry.Properties.Add("component", componentName); 
      telemetry.Properties.Add("method", method);     
      telemetry.Properties.Add("timespan", timespan.ToString()); 

      if (!string.IsNullOrWhiteSpace(properties)) 
       telemetry.Properties.Add("properties", properties); 

      APM.TrackTrace(telemetry); 
     } 
    } 

    public static void TraceMethod(string currentNameSpace, string componentName, string method, TimeSpan timespan, IDictionary<string, string> properties = null) 
    { 
     lock (theThreadLock) 
     { 
      var telemetry = new TraceTelemetry("Trace API call", SeverityLevel.Information); 
      telemetry.Properties.Add("namespace", currentNameSpace); 
      telemetry.Properties.Add("component", componentName); 
      telemetry.Properties.Add("method", method); 
      telemetry.Properties.Add("timespan", timespan.ToString()); 

      if (properties != null) 
      { 
       foreach (var property in properties) 
       { 
        telemetry.Properties.Add(property.Key, property.Value); 
       } 
      } 

      APM.TrackTrace(telemetry); 
     } 
    } 
    public static void TrackEvent(string message) 
    { 
     lock (theThreadLock) 
     { 
      APM.TrackEvent(message); 
     } 
    } 

    public static void TrackEvent(string message, IDictionary<string, string> properties) 
    { 
     lock (theThreadLock) 
     { 
      var telemetry = new TraceTelemetry("Track Event", SeverityLevel.Information); 
      telemetry.Message = message; 

      if (properties != null) 
      { 
       foreach (var property in properties) 
       { 
        telemetry.Properties.Add(property.Key, property.Value); 
       } 
      } 

      APM.TrackEvent(message); 
     } 
    } 
} 

public sealed class LoggerEvents 
{ 
    private readonly string name; 
    private readonly int value; 

    public readonly static LoggerEvents ErrorWebSite = new LoggerEvents(1, "Error:WebSite"); 
    public readonly static LoggerEvents UnhandledException = new LoggerEvents(2, "UnhandledException"); 
    public readonly static LoggerEvents UserNotActive = new LoggerEvents(3, "UserNotActive"); 
    public readonly static LoggerEvents InvalidURL = new LoggerEvents(4, "InvalidURL"); 
    public readonly static LoggerEvents ConsentRefused = new LoggerEvents(5,"ConsentRefused"); 
    public readonly static LoggerEvents ConsentAccepted = new LoggerEvents(6, "ConsentAccepted"); 
    public readonly static LoggerEvents UserLoginSuccess = new LoggerEvents(7, "LoginSuccess"); 
    public readonly static LoggerEvents UserLoginFailure = new LoggerEvents(7, "LoginFailure"); 
    private LoggerEvents(int value, string name) 
    { 
     this.name = name; 
     this.value = value; 
    } 

    public override string ToString() 
    { 
     return name; 
    } 
} 
+0

我的擔心不是將異常信息發送給ApplicationInsights。我已經做到了。 – ak92

+0

示例類具有關於如何發送額外數據,屬性和使用異常遙測的示例。 – aaronR

0

我能夠使用邏輯應用程序中的分析查詢獲取異常詳細信息。 我運行了一個查詢,它在最近5分鐘內獲取了所有異常。 然後我將這個HTTP動作傳遞給了我的API以作進一步處理。