首页 文章

在Application Insights webhook JSON中获取异常详细信息

提问于
浏览
0

我已将Application Insights警报配置为在服务器例外上触发 . 我正在通过逻辑应用程序处理此警报,该工具运行正常 .

我想编辑警报有效负载JSON,以便它包含其中的异常详细信息 . 从可用的documentation上设置警报,我们似乎可以编辑属性以包含一些自定义数据,这些数据可以使用Azure Monitor API进行设置

Azure Monitor API Alert Rules Documentation没有提及太多 .

有什么建议吗?

2 回答

  • 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

    我能够使用Logic App中的Analytics Query获取异常详细信息 . 我运行了一个查询,它在最后5分钟内获取了所有异常 . 然后我使用HTTP操作将其传递给我的API以进行进一步处理 .

相关问题