Esempio n. 1
0
        private void AddTraceFrames(StringException ex)
        {
            StackTrace = ex.TraceFrames;

            if (StackTrace != null && StackTrace.Count > 0)
            {
                this.SourceMethod = ex.TraceFrames[0].Method;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Emit the provided log event to the sink.
        /// </summary>
        /// <param name="logEvent">The log event to write.</param>
        public void Emit(LogEvent logEvent)
        {
            if (StackifyLib.Logger.PrefixEnabled() || StackifyLib.Logger.CanSend())
            {
                var msg = new LogMsg()
                {
                    Level = LevelToSeverity(logEvent),
                    Msg   = logEvent.RenderMessage(_formatProvider),
                    data  = PropertiesToData(logEvent)
                };

                StackifyError error = null;

                Exception ex = logEvent.Exception;

                if (ex == null)
                {
                    if (logEvent.Level == LogEventLevel.Error || logEvent.Level == LogEventLevel.Fatal)
                    {
                        StringException stringException = new StringException(msg.Msg);
                        stringException.TraceFrames = StackifyLib.Logger.GetCurrentStackTrace(null);
                        error = StackifyError.New(stringException);
                    }
                }
                else if (ex is StackifyError)
                {
                    error = (StackifyError)ex;
                }
                else
                {
                    error = StackifyError.New(ex);
                }

                if (error != null && !StackifyError.IgnoreError(error) && _Governor.ErrorShouldBeSent(error))
                {
                    msg.Ex = error;
                }

                _logClient.QueueMessage(msg);
            }
        }
Esempio n. 3
0
        internal LogMsg Translate(LogEventInfo loggingEvent)
        {
            if (loggingEvent == null)
            {
                return(null);
            }

            //do not log our own messages. This is to prevent any sort of recursion that could happen since calling to send this will cause even more logging to happen
            if (loggingEvent.FormattedMessage != null && loggingEvent.FormattedMessage.IndexOf("StackifyLib:", StringComparison.OrdinalIgnoreCase) > -1)
            {
                return(null);
            }

            StackifyLib.Models.LogMsg msg = new LogMsg();


            if (loggingEvent.Level != null)
            {
                msg.Level = loggingEvent.Level.Name;
            }



            if (loggingEvent.HasStackTrace && loggingEvent.UserStackFrame != null)
            {
                var frame = loggingEvent.UserStackFrame;

                MethodBase method = frame.GetMethod();
                if (method != (MethodBase)null && method.DeclaringType != (Type)null)
                {
                    if (method.DeclaringType != (Type)null)
                    {
                        msg.SrcMethod = method.DeclaringType.FullName + "." + method.Name;
                        msg.SrcLine   = frame.GetFileLineNumber();
                    }
                }
            }


            //if it wasn't set above for some reason we will do it this way as a fallback
            if (string.IsNullOrEmpty(msg.SrcMethod))
            {
                msg.SrcMethod = loggingEvent.LoggerName;

                if ((logMethodNames ?? false))
                {
                    var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true);

                    if (frames.Any())
                    {
                        var first = frames.First();

                        msg.SrcMethod = first.Method;
                        msg.SrcLine   = first.LineNum;
                    }
                }
            }

            string formattedMessage;

            //Use the layout render to allow custom fields to be logged, but not if it is the default format as it logs a bunch fields we already log
            //really no reason to use a layout at all
            if (this.Layout != null && this.Layout.ToString() != "'${longdate}|${level:uppercase=true}|${logger}|${message}'") //do not use if it is the default
            {
                formattedMessage = this.Layout.Render(loggingEvent);
            }
            else
            {
                formattedMessage = loggingEvent.FormattedMessage;
            }

            msg.Msg = (formattedMessage ?? "").Trim();

            object debugObject = null;
            Dictionary <string, object> args = new Dictionary <string, object>();

            if ((loggingEvent.Parameters != null) && (loggingEvent.Parameters.Length > 0))
            {
                for (int i = 0; i < loggingEvent.Parameters.Length; i++)
                {
                    var item = loggingEvent.Parameters[i];

                    if (item == null)
                    {
                        continue;
                    }
                    else if (item is Exception)
                    {
                        if (loggingEvent.Exception == null)
                        {
                            loggingEvent.Exception = (Exception)item;
                        }
                    }
                    else if (item.ToString() == msg.Msg)
                    {
                        //ignore it.
                    }
                    else if (logAllParams ?? true)
                    {
                        args["arg" + i] = loggingEvent.Parameters[i];
                        debugObject     = item;
                    }
                    else
                    {
                        debugObject = item;
                    }
                }

                if ((logAllParams ?? true) && args != null && args.Count > 1)
                {
                    debugObject = args;
                }
            }


            StackifyError error = null;

            if (loggingEvent.Exception != null && loggingEvent.Exception is StackifyError)
            {
                error = (StackifyError)loggingEvent.Exception;
            }
            else if (loggingEvent.Exception != null)
            {
                error = StackifyError.New((Exception)loggingEvent.Exception);
            }

            var diags = GetDiagnosticContextProperties();

            if (diags != null && diags.ContainsKey("transid"))
            {
                msg.TransID = diags["transid"].ToString();
                diags.Remove("transid");
            }



            if (debugObject != null)
            {
                msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(debugObject, true, diags);
            }
            else
            {
                msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags);
            }


            if (msg.Msg != null && error != null)
            {
                msg.Msg += "\r\n" + error.ToString();
            }
            else if (msg.Msg == null && error != null)
            {
                msg.Msg = error.ToString();
            }

            if (error == null && (loggingEvent.Level == LogLevel.Error || loggingEvent.Level == LogLevel.Fatal))
            {
                StringException stringException = new StringException(msg.Msg);

                stringException.TraceFrames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName);

                if (!loggingEvent.HasStackTrace || loggingEvent.UserStackFrame == null)
                {
                    if (stringException.TraceFrames.Any())
                    {
                        var first = stringException.TraceFrames.First();

                        msg.SrcMethod = first.Method;
                        msg.SrcLine   = first.LineNum;
                    }
                }

                //Make error out of log message
                error = StackifyError.New(stringException);
            }

            if (error != null && !StackifyError.IgnoreError(error) && _logClient.ErrorShouldBeSent(error))
            {
                error.SetAdditionalMessage(formattedMessage);
                msg.Ex = error;
            }
            else if (error != null && msg.Msg != null)
            {
                msg.Msg += " #errorgoverned";
            }


            return(msg);
        }
        internal LogMsg Translate(LoggingEvent loggingEvent)
        {
            if (loggingEvent == null)
            {
                return(null);
            }

            //don't log our own logging causing a loop
            if (loggingEvent.RenderedMessage.IndexOf("StackifyLib:", StringComparison.OrdinalIgnoreCase) > -1)
            {
                return(null);
            }


            var msg = new LogMsg();


            if (loggingEvent.Level != null)
            {
                msg.Level = loggingEvent.Level.DisplayName;
            }

            try
            {
                msg.SrcMethod = loggingEvent.LoggerName;

                if (logMethodNames ?? false)
                {
                    var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true);

                    if (frames.Any())
                    {
                        var first = frames.First();

                        msg.SrcMethod = first.Method;
                        msg.SrcLine   = first.LineNum;
                    }
                }
            }
            catch (Exception ex)
            {
                StackifyAPILogger.Log("Error evaluating source method " + ex.ToString());
            }

            var diags = GetDiagnosticContextProperties();


            if (diags != null && diags.ContainsKey("transid"))
            {
                msg.TransID = diags["transid"].ToString();
                diags.Remove("transid");
            }

            StackifyError error                  = null;
            object        messageObject          = null;
            string        errorAdditionalMessage = null;

            if (loggingEvent.MessageObject != null && loggingEvent.MessageObject is StackifyError)
            {
                //Message Object was an exception

                error                  = (StackifyError)loggingEvent.MessageObject;
                messageObject          = error.ToString();
                errorAdditionalMessage = null;
            }
            else if (loggingEvent.MessageObject != null && loggingEvent.MessageObject is Exception)
            {
                //Message Object was an exception

                error                  = StackifyError.New((Exception)loggingEvent.MessageObject);
                messageObject          = error.ToString();
                errorAdditionalMessage = null;
            }
            else if (loggingEvent.ExceptionObject != null)
            {
                //Exception was passed in

                if (loggingEvent.ExceptionObject is StackifyError)
                {
                    error = loggingEvent.ExceptionObject as StackifyError;
                }
                else
                {
                    error = StackifyError.New(loggingEvent.ExceptionObject);
                }

                errorAdditionalMessage = loggingEvent.RenderedMessage;
                messageObject          = loggingEvent.MessageObject;
            }
            else
            {
                messageObject = loggingEvent.MessageObject;
            }


            //messageObject is not an object we need to serialize.
            if (messageObject == null || messageObject is string || messageObject.GetType().FullName == "log4net.Util.SystemStringFormat")
            {
                //passing null to the serialize object since we can't serialize the logged object. We only need to get potential diags.
                msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags);
                msg.Msg  = loggingEvent.RenderedMessage;
            }
            else if (messageObject is StackifyLib.Models.LogMessage)
            {
                var item = messageObject as StackifyLib.Models.LogMessage;

                msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(item.json, true, diags);
                msg.Msg  = item.message;
            }
            else
            {
                //try to serialize the messageObject since we know its not a string
                msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(messageObject, false, diags);
                msg.Msg  = loggingEvent.RenderedMessage;
            }

            if (error != null)
            {
                msg.Msg += "\r\n" + error.ToString();
            }

            if (!string.IsNullOrWhiteSpace(errorAdditionalMessage) && error != null)
            {
                //if something besides just the exception was logged, add that message to our error object
                error.SetAdditionalMessage(errorAdditionalMessage);
            }
            else if (msg.Msg == null && error != null)
            {
                msg.Msg = error.ToString();
            }

            if (error == null && (loggingEvent.Level == Level.Error || loggingEvent.Level == Level.Fatal))
            {
                StringException stringEx = new StringException(loggingEvent.RenderedMessage);
                stringEx.TraceFrames = new List <TraceFrame>();


                stringEx.TraceFrames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName);

                if (stringEx.TraceFrames.Any())
                {
                    var first = stringEx.TraceFrames.First();

                    msg.SrcMethod = first.Method;
                    msg.SrcLine   = first.LineNum;
                }

                //Make error out of log message
                error = StackifyError.New(stringEx);
            }

            if (error != null && !StackifyError.IgnoreError(error) && _logClient.ErrorShouldBeSent(error))
            {
                msg.Ex = error;
            }
            else if (error != null && msg.Msg != null)
            {
                msg.Msg += " #errorgoverned";
            }
            return(msg);
        }
        internal LogMsg Translate(LoggingEvent loggingEvent)
        {

            if (loggingEvent == null)
                return null;

            //don't log our own logging causing a loop
            if (loggingEvent.RenderedMessage.IndexOf("StackifyLib:", StringComparison.OrdinalIgnoreCase) > -1)
                return null;


            var msg = new LogMsg();


            if (loggingEvent.Level != null)
            {
                msg.Level = loggingEvent.Level.DisplayName;
            }

            try
            {
                msg.SrcMethod = loggingEvent.LoggerName;

                if (logMethodNames ?? false)
                {
                    var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true);

                    if (frames.Any())
                    {
                        var first = frames.First();

                        msg.SrcMethod = first.Method;
                        msg.SrcLine = first.LineNum;
                    }
                }
            }
            catch (Exception ex)
            {
                StackifyAPILogger.Log("Errror evaluating source method " + ex.ToString());
            }

            var diags = GetDiagnosticContextProperties();


            if (diags != null && diags.ContainsKey("transid"))
            {
                msg.TransID = diags["transid"].ToString();
                diags.Remove("transid");
            }

            StackifyError error = null;
            object messageObject = null;
            string errorAdditionalMessage = null;

            if (loggingEvent.MessageObject != null && loggingEvent.MessageObject is StackifyError)
            {
                //Message Object was an exception

                error = (StackifyError)loggingEvent.MessageObject;
                messageObject = error.ToString();
                errorAdditionalMessage = null;
            }
            else if (loggingEvent.MessageObject != null && loggingEvent.MessageObject is Exception)
            {
                //Message Object was an exception

                error = StackifyError.New((Exception)loggingEvent.MessageObject);
                messageObject = error.ToString();
                errorAdditionalMessage = null;

            }
            else if (loggingEvent.ExceptionObject != null)
            {
                //Exception was passed in

                if (loggingEvent.ExceptionObject is StackifyError)
                {
                    error = loggingEvent.ExceptionObject as StackifyError;
                }
                else
                {
                    error = StackifyError.New(loggingEvent.ExceptionObject);
                }

                errorAdditionalMessage = loggingEvent.RenderedMessage;
                messageObject = loggingEvent.MessageObject;
            }
            else
            {
                messageObject = loggingEvent.MessageObject;
            }


            //messageObject is not an object we need to serialize.
            if (messageObject == null || messageObject is string || messageObject.GetType().FullName == "log4net.Util.SystemStringFormat")
            {
                //passing null to the serialize object since we can't serialize the logged object. We only need to get potential diags.
                msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags);
                msg.Msg = loggingEvent.RenderedMessage;
            }
            else if (messageObject is StackifyLib.Models.LogMessage)
            {
                var item = messageObject as StackifyLib.Models.LogMessage;

                msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(item.json, true, diags);
                msg.Msg = item.message;
            }
            else
            {
                //try to serialize the messageObject since we know its not a string
                msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(messageObject, false, diags);
                msg.Msg = loggingEvent.RenderedMessage;

                if (error != null)
                {
                    msg.Msg += "\r\n" + error.ToString();
                }
            }



            if (!string.IsNullOrWhiteSpace(errorAdditionalMessage) && error != null)
            {
                //if something besides just the exception was logged, add that message to our error object
                error.SetAdditionalMessage(errorAdditionalMessage);
            }
            else if (msg.Msg == null && error != null)
            {
                msg.Msg = error.ToString();
            }

            if (error == null && (loggingEvent.Level == Level.Error || loggingEvent.Level == Level.Fatal))
            {
                StringException stringEx = new StringException(loggingEvent.RenderedMessage);
                stringEx.TraceFrames = new List<TraceFrame>();


                stringEx.TraceFrames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName);

                if (stringEx.TraceFrames.Any())
                {
                    var first = stringEx.TraceFrames.First();

                    msg.SrcMethod = first.Method;
                    msg.SrcLine = first.LineNum;
                }

                //Make error out of log message
                error = StackifyError.New(stringEx);
            }

            if (error != null && !StackifyError.IgnoreError(error) && _logClient.ErrorShouldBeSent(error))
            {
                msg.Ex = error;
            }
            else if (error != null && msg.Msg != null)
            {
                msg.Msg += " #errorgoverned";
            }
            return msg;
        }
Esempio n. 6
0
        internal LogMsg Translate(LogEventInfo loggingEvent)
        {
            if (loggingEvent == null)
            {
                return(null);
            }

            //do not log our own messages. This is to prevent any sort of recursion that could happen since calling to send this will cause even more logging to happen
            if (loggingEvent.FormattedMessage != null && loggingEvent.FormattedMessage.IndexOf("StackifyLib:", StringComparison.OrdinalIgnoreCase) > -1)
            {
                return(null);
            }

            StackifyLib.Models.LogMsg msg = new LogMsg();

            if (loggingEvent.Level != null)
            {
                msg.Level = loggingEvent.Level.Name;
            }

            if (!string.IsNullOrEmpty(loggingEvent.CallerMemberName))
            {
                msg.SrcMethod = loggingEvent.CallerMemberName;
                msg.SrcLine   = loggingEvent.CallerLineNumber;
            }

            //if it wasn't set above for some reason we will do it this way as a fallback
            if (string.IsNullOrEmpty(msg.SrcMethod))
            {
                msg.SrcMethod = loggingEvent.LoggerName;

                if ((logMethodNames ?? false))
                {
                    var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true);

                    if (frames.Any())
                    {
                        var first = frames.First();

                        msg.SrcMethod = first.Method;
                        msg.SrcLine   = first.LineNum;
                    }
                }
            }

            msg.Msg = RenderLogEvent(Layout, loggingEvent) ?? string.Empty;

            var contextProperties             = GetContextProperties(loggingEvent);
            Dictionary <string, object> diags = contextProperties as Dictionary <string, object> ?? new Dictionary <string, object>(contextProperties);

            if (diags.TryGetValue("ndc", out var topFrame) && string.IsNullOrEmpty((topFrame as string)))
            {
                diags.Remove("ndc");
            }

            if (diags.TryGetValue("transid", out var transId))
            {
                msg.TransID = diags["transid"].ToString();
                diags.Remove("transid");
            }

#if NETFULL
            foreach (string key in _CallContextKeys)
            {
                object value = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData(key);
                if (value != null)
                {
                    diags[key.ToLower()] = value;
                }
            }
#endif

            StackifyError stackifyError = loggingEvent.Exception != null?StackifyError.New(loggingEvent.Exception) : null;

            object debugObject = null;
            if (IncludeEventProperties && loggingEvent.HasProperties)
            {
                Dictionary <string, object> args = new Dictionary <string, object>(loggingEvent.Properties.Count);
                foreach (KeyValuePair <object, object> eventProperty in loggingEvent.Properties)
                {
                    string propertyKey = eventProperty.Key.ToString();
                    if (!string.IsNullOrEmpty(propertyKey))
                    {
                        args[propertyKey] = eventProperty.Value;
                    }
                }

                if ((logAllParams ?? false) && loggingEvent.Parameters != null && loggingEvent.Parameters.Length > 0)
                {
                    debugObject = CaptureParameters(loggingEvent, msg.Msg, args);
                }
                else
                {
                    debugObject = args;
                }
            }

            if (loggingEvent.Parameters?.Length > 0)
            {
                for (int i = 0; i < loggingEvent.Parameters.Length; i++)
                {
                    var parameter = loggingEvent.Parameters[i];
                    if (stackifyError == null && parameter is StackifyError error)
                    {
                        stackifyError = error;
                    }
                    if (debugObject == null && parameter is LogMessage debugMessage)
                    {
                        debugObject = debugMessage.json;
                    }
                }

                if (debugObject == null)
                {
                    Dictionary <string, object> args = ((logAllParams ?? true) && loggingEvent.Parameters.Length > 1) ? new Dictionary <string, object>() : null;
                    debugObject = CaptureParameters(loggingEvent, msg.Msg, args);
                }
            }

            if (debugObject != null)
            {
                msg.data = HelperFunctions.SerializeDebugData(debugObject, true, diags);
            }
            else
            {
                msg.data = HelperFunctions.SerializeDebugData(null, false, diags);
            }

            if (stackifyError == null && (loggingEvent.Level == LogLevel.Error || loggingEvent.Level == LogLevel.Fatal))
            {
                StringException stringException = new StringException(msg.Msg);
                if ((logMethodNames ?? false))
                {
                    stringException.TraceFrames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName);
                }
                stackifyError = StackifyError.New(stringException);
            }

            if (stackifyError != null && !StackifyError.IgnoreError(stackifyError) && _logClient.ErrorShouldBeSent(stackifyError))
            {
                stackifyError.SetAdditionalMessage(loggingEvent.FormattedMessage);
                msg.Ex = stackifyError;
            }
            else if (stackifyError != null && msg.Msg != null)
            {
                msg.Msg += " #errorgoverned";
            }

            return(msg);
        }
Esempio n. 7
0
        private void AddTraceFrames(StringException ex)
        {
            StackTrace = ex.TraceFrames;

            if (StackTrace != null && StackTrace.Count > 0)
            {
                this.SourceMethod = ex.TraceFrames[0].Method;
            }
        }