SetStackTrace() 공개 메소드

Sets the stack trace for the event info.
public SetStackTrace ( Windows.Adapters.StackTrace stackTrace, int userStackFrame ) : void
stackTrace Windows.Adapters.StackTrace The stack trace.
userStackFrame int Index of the first user stack frame within the stack trace.
리턴 void
예제 #1
0
        private void ProcessLogEventInfo(LogLevel logLevel, string loggerName, [Localizable(false)] string message, object[] arguments, int?eventId)
        {
            var ev = new LogEventInfo();

            ev.LoggerName = (loggerName ?? this.Name) ?? string.Empty;

#if !NET_CF
            if (this.AutoLoggerName)
            {
                var        stack          = new StackTrace();
                int        userFrameIndex = -1;
                MethodBase userMethod     = null;

                for (int i = 0; i < stack.FrameCount; ++i)
                {
                    var frame  = stack.GetFrame(i);
                    var method = frame.GetMethod();

                    if (method.DeclaringType == this.GetType())
                    {
                        // skip all methods of this type
                        continue;
                    }

                    if (method.DeclaringType.Assembly == systemAssembly)
                    {
                        // skip all methods from System.dll
                        continue;
                    }

                    userFrameIndex = i;
                    userMethod     = method;
                    break;
                }

                if (userFrameIndex >= 0)
                {
                    ev.SetStackTrace(stack, userFrameIndex);
                    if (userMethod.DeclaringType != null)
                    {
                        ev.LoggerName = userMethod.DeclaringType.FullName;
                    }
                }
            }
#endif

            ev.TimeStamp  = CurrentTimeGetter.Now;
            ev.Message    = message;
            ev.Parameters = arguments;
            ev.Level      = this.forceLogLevel ?? logLevel;

            if (eventId.HasValue)
            {
                ev.Properties.Add("EventID", eventId.Value);
            }

            Logger logger = LogManager.GetLogger(ev.LoggerName);
            logger.Log(ev);
        }
예제 #2
0
 public static void Fatal(Exception ex, string module)
 {
     lock (_sync)
     {
         var log = ServiceLocator.Current.GetInstance <ILogger>();
         var e   = new NLog.LogEventInfo();
         e.Level                = LogLevel.Fatal;
         e.Exception            = ex;
         e.Properties["module"] = module;
         e.SetStackTrace(new StackTrace(), 1);
         log.Log(e);
     }
 }
예제 #3
0
파일: LoggerImpl.cs 프로젝트: fxmozart/NLog
        internal static void Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
        {
            if (targets == null)
            {
                return;
            }

#if !NET_CF
            StackTraceUsage stu = targets.GetStackTraceUsage();

            if (stu != StackTraceUsage.None && !logEvent.HasStackTrace)
            {
                StackTrace stackTrace;
#if !SILVERLIGHT
                stackTrace = new StackTrace(StackTraceSkipMethods, stu == StackTraceUsage.WithSource);
#else
                stackTrace = new StackTrace();
#endif

                int firstUserFrame = FindCallingMethodOnStackTrace(stackTrace, loggerType);

                logEvent.SetStackTrace(stackTrace, firstUserFrame);
            }
#endif

            int originalThreadId = Thread.CurrentThread.ManagedThreadId;
            AsyncContinuation exceptionHandler = ex =>
                {
                    if (ex != null)
                    {
                        if (factory.ThrowExceptions && Thread.CurrentThread.ManagedThreadId == originalThreadId)
                        {
                            throw new NLogRuntimeException("Exception occurred in NLog", ex);
                        }
                    }
                };

            for (var t = targets; t != null; t = t.NextInChain)
            {
                if (!WriteToTargetWithFilterChain(t, logEvent, exceptionHandler))
                {
                    break;
                }
            }



            // Destroy the objects
            logEvent.Dispose();

        }
예제 #4
0
        internal static void Write([NotNull] Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
        {
            if (targets == null)
            {
                return;
            }

            StackTraceUsage stu = targets.GetStackTraceUsage();

            if (stu != StackTraceUsage.None && !logEvent.HasStackTrace)
            {
                StackTrace stackTrace;
#if NETSTANDARD1_5
                stackTrace = (StackTrace)Activator.CreateInstance(typeof(StackTrace), new object[] { stu == StackTraceUsage.WithSource });
#elif !SILVERLIGHT
                stackTrace = new StackTrace(StackTraceSkipMethods, stu == StackTraceUsage.WithSource);
#else
                stackTrace = new StackTrace();
#endif

                int firstUserFrame = FindCallingMethodOnStackTrace(stackTrace, loggerType);

                logEvent.SetStackTrace(stackTrace, firstUserFrame);
            }

            AsyncContinuation exceptionHandler = (ex) => { };
            if (factory.ThrowExceptions)
            {
                int originalThreadId = Thread.CurrentThread.ManagedThreadId;
                exceptionHandler = ex =>
                {
                    if (ex != null)
                    {
                        if (Thread.CurrentThread.ManagedThreadId == originalThreadId)
                        {
                            throw new NLogRuntimeException("Exception occurred in NLog", ex);
                        }
                    }
                };
            }

            for (var t = targets; t != null; t = t.NextInChain)
            {
                if (!WriteToTargetWithFilterChain(t, logEvent, exceptionHandler))
                {
                    break;
                }
            }
        }
예제 #5
0
        internal static void Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
        {
            InternalLogger.Trace("Writing event {0} {1} {2}", logEvent.LoggerName, logEvent.Level, logEvent.Message);

            if (targets == null)
            {
                return;
            }

            StackTraceUsage stu = targets.GetStackTraceUsage();

            if (stu != StackTraceUsage.None && !logEvent.HasStackTrace)
            {
                StackTrace stackTrace;
#if !SILVERLIGHT
                stackTrace = new StackTrace(StackTraceSkipMethods, stu == StackTraceUsage.WithSource);
#else
                stackTrace = new StackTrace();
#endif

                int firstUserFrame = FindCallingMethodOnStackTrace(stackTrace, loggerType);

                logEvent.SetStackTrace(stackTrace, firstUserFrame);
            }

            int originalThreadId = Thread.CurrentThread.ManagedThreadId;
            AsyncContinuation exceptionHandler = ex =>
            {
                if (ex != null)
                {
                    if (factory.ThrowExceptions && Thread.CurrentThread.ManagedThreadId == originalThreadId)
                    {
                        throw new NLogRuntimeException("Exception occurred in NLog", ex);
                    }
                }
            };

            for (var t = targets; t != null; t = t.NextInChain)
            {
                InternalLogger.Trace("Writing to logger {0}", t.Target.Name);
                if (!WriteToTargetWithFilterChain(t, logEvent, exceptionHandler))
                {
                    break;
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Process the log event
        /// <param name="logLevel">The log level.</param>
        /// <param name="loggerName">The name of the logger.</param>
        /// <param name="message">The log message.</param>
        /// <param name="arguments">The log parameters.</param>
        /// <param name="eventId">The event id.</param>
        /// <param name="eventType">The event type.</param>
        /// <param name="relatedActiviyId">The related activity id.</param>
        /// </summary>
        protected virtual void ProcessLogEventInfo(LogLevel logLevel, string loggerName, [Localizable(false)] string message, object[] arguments, int?eventId, TraceEventType?eventType, Guid?relatedActiviyId)
        {
            StackTrace stackTrace = AutoLoggerName ? new StackTrace() : null;
            ILogger    logger     = GetLogger(loggerName, stackTrace, out int userFrameIndex);

            logLevel = _forceLogLevel ?? logLevel;
            if (!logger.IsEnabled(logLevel))
            {
                return; // We are done
            }

            var ev = new LogEventInfo();

            ev.LoggerName = logger.Name;
            ev.Level      = logLevel;
            if (eventType.HasValue)
            {
                ev.Properties.Add("EventType", eventType.Value);
            }

            if (relatedActiviyId.HasValue)
            {
                ev.Properties.Add("RelatedActivityID", relatedActiviyId.Value);
            }

            ev.Message    = message;
            ev.Parameters = arguments;
            ev.Level      = _forceLogLevel ?? logLevel;

            if (eventId.HasValue)
            {
                ev.Properties.Add("EventID", eventId.Value);
            }

            if (stackTrace != null && userFrameIndex >= 0)
            {
                ev.SetStackTrace(stackTrace, userFrameIndex);
            }

            logger.Log(ev);
        }
예제 #7
0
        internal static void Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
        {
            if (targets == null)
                return;

#if !NETCF            
            bool needTrace = false;
            bool needTraceSources = false;

            int nst = targets.NeedsStackTrace;

            if (nst > 0)
                needTrace = true;
            if (nst > 1)
                needTraceSources = true;

            StackTrace stackTrace = null;
            if (needTrace && !logEvent.HasStackTrace)
            {
                int firstUserFrame = 0;
                stackTrace = new StackTrace(STACK_TRACE_SKIP_METHODS, needTraceSources);

                for (int i = 0; i < stackTrace.FrameCount; ++i)
                {
                    System.Reflection.MethodBase mb = stackTrace.GetFrame(i).GetMethod();

                    if (mb.DeclaringType == loggerType)
                    {
                        firstUserFrame = i + 1;
                    }
                    else
                    {
                        if (firstUserFrame != 0)
                            break;
                    }
                }
                logEvent.SetStackTrace(stackTrace, firstUserFrame);
            }
#endif 
            for (TargetWithFilterChain awf = targets; awf != null; awf = awf.Next)
            {
                Target app = awf.Target;
                FilterResult result = FilterResult.Neutral;

                try
                {
                    FilterCollection filterChain = awf.FilterChain;

                    for (int i = 0; i < filterChain.Count; ++i)
                    {
                        Filter f = filterChain[i];
                        result = f.Check(logEvent);
                        if (result != FilterResult.Neutral)
                            break;
                    }
                    if ((result == FilterResult.Ignore) || (result == FilterResult.IgnoreFinal))
                    {
                        if (InternalLogger.IsDebugEnabled)
                        {
                            InternalLogger.Debug("{0}.{1} Rejecting message because of a filter.", logEvent.LoggerName, logEvent.Level);
                        }
                        if (result == FilterResult.IgnoreFinal)
                            return;
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    InternalLogger.Error("FilterChain exception: {0}", ex);
                    if (factory.ThrowExceptions)
                        throw;
                    else
                        continue;
                }

                try
                {
                    app.Write(logEvent);
                }
                catch (Exception ex)
                {
                    InternalLogger.Error("Target exception: {0}", ex);
                    if (factory.ThrowExceptions)
                        throw;
                    else
                        continue;
                }
                if (result == FilterResult.LogFinal)
                    return;
            }
        }
예제 #8
0
        protected virtual void ProcessLogEventInfo(LogLevel logLevel, string loggerName, [Localizable(false)] string message, object[] arguments, int? eventId)
#endif
        {
            var ev = new LogEventInfo();

            ev.LoggerName = (loggerName ?? this.Name) ?? string.Empty;
            
#if !NET_CF
            if (this.AutoLoggerName)
            {
                var stack = new StackTrace();
                int userFrameIndex = -1;
                MethodBase userMethod = null;

                for (int i = 0; i < stack.FrameCount; ++i)
                {
                    var frame = stack.GetFrame(i);
                    var method = frame.GetMethod();

                    if (method.DeclaringType == this.GetType())
                    {
                        // skip all methods of this type
                        continue;
                    }

                    if (method.DeclaringType.Assembly == systemAssembly)
                    {
                        // skip all methods from System.dll
                        continue;
                    }

                    userFrameIndex = i;
                    userMethod = method;
                    break;
                }

                if (userFrameIndex >= 0)
                {
                    ev.SetStackTrace(stack, userFrameIndex);
                    if (userMethod.DeclaringType != null)
                    {
                        ev.LoggerName = userMethod.DeclaringType.FullName;
                    }
                }
            }

            if (eventType.HasValue)
            {
                ev.Properties.Add("EventType", eventType.Value);
            }

            if (relatedActiviyId.HasValue)
            {
                ev.Properties.Add("RelatedActivityID", relatedActiviyId.Value);
            }
#endif

            ev.TimeStamp = TimeSource.Current.Time;
            ev.Message = message;
            ev.Parameters = arguments;
            ev.Level = this.forceLogLevel ?? logLevel;

            if (eventId.HasValue)
            {
                ev.Properties.Add("EventID", eventId.Value);
            }

            Logger logger;
            if (this.LogFactory != null)
            {
                logger = this.LogFactory.GetLogger(ev.LoggerName);
            }
            else
            {
                logger = LogManager.GetLogger(ev.LoggerName);
            }

            logger.Log(ev);
        }
예제 #9
0
        internal static void Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
        {
            if (targets == null)
            {
                return;
            }

#if !NETCF
            bool needTrace        = false;
            bool needTraceSources = false;

            int nst = targets.NeedsStackTrace;

            if (nst > 0)
            {
                needTrace = true;
            }
            if (nst > 1)
            {
                needTraceSources = true;
            }

            StackTrace stackTrace = null;
            if (needTrace && !logEvent.HasStackTrace)
            {
                int firstUserFrame = 0;
                stackTrace = new StackTrace(STACK_TRACE_SKIP_METHODS, needTraceSources);

                for (int i = 0; i < stackTrace.FrameCount; ++i)
                {
                    System.Reflection.MethodBase mb = stackTrace.GetFrame(i).GetMethod();

                    if (mb.DeclaringType == loggerType)
                    {
                        firstUserFrame = i + 1;
                    }
                    else
                    {
                        if (firstUserFrame != 0)
                        {
                            break;
                        }
                    }
                }
                logEvent.SetStackTrace(stackTrace, firstUserFrame);
            }
#endif
            for (TargetWithFilterChain awf = targets; awf != null; awf = awf.Next)
            {
                Target       app    = awf.Target;
                FilterResult result = FilterResult.Neutral;

                try
                {
                    FilterCollection filterChain = awf.FilterChain;

                    for (int i = 0; i < filterChain.Count; ++i)
                    {
                        Filter f = filterChain[i];
                        result = f.Check(logEvent);
                        if (result != FilterResult.Neutral)
                        {
                            break;
                        }
                    }
                    if ((result == FilterResult.Ignore) || (result == FilterResult.IgnoreFinal))
                    {
                        if (InternalLogger.IsDebugEnabled)
                        {
                            InternalLogger.Debug("{0}.{1} Rejecting message because of a filter.", logEvent.LoggerName, logEvent.Level);
                        }
                        if (result == FilterResult.IgnoreFinal)
                        {
                            return;
                        }
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    InternalLogger.Error("FilterChain exception: {0}", ex);
                    if (factory.ThrowExceptions)
                    {
                        throw;
                    }
                    else
                    {
                        continue;
                    }
                }

                try
                {
                    app.Write(logEvent);
                }
                catch (Exception ex)
                {
                    InternalLogger.Error("Target exception: {0}", ex);
                    if (factory.ThrowExceptions)
                    {
                        throw;
                    }
                    else
                    {
                        continue;
                    }
                }
                if (result == FilterResult.LogFinal)
                {
                    return;
                }
            }
        }
예제 #10
0
        /// <summary>
        /// Process the log event
        /// <param name="logLevel">The log level.</param>
        /// <param name="loggerName">The name of the logger.</param>
        /// <param name="message">The log message.</param>
        /// <param name="arguments">The log parameters.</param>
        /// <param name="eventId">The event id.</param>
        /// <param name="eventType">The event type.</param>
        /// <param name="relatedActiviyId">The related activity id.</param>
        /// </summary>
        protected virtual void ProcessLogEventInfo(LogLevel logLevel, string loggerName, [Localizable(false)] string message, object[] arguments, int?eventId, TraceEventType?eventType, Guid?relatedActiviyId)
        {
            loggerName = (loggerName ?? Name) ?? string.Empty;

            StackTrace stackTrace     = null;
            int        userFrameIndex = -1;

            if (AutoLoggerName)
            {
                stackTrace = new StackTrace();
                MethodBase userMethod = null;

                for (int i = 0; i < stackTrace.FrameCount; ++i)
                {
                    var frame  = stackTrace.GetFrame(i);
                    var method = frame.GetMethod();

                    if (method.DeclaringType == GetType())
                    {
                        // skip all methods of this type
                        continue;
                    }

                    if (method.DeclaringType != null && method.DeclaringType.Assembly == systemAssembly)
                    {
                        // skip all methods from System.dll
                        continue;
                    }

                    userFrameIndex = i;
                    userMethod     = method;
                    break;
                }

                if (userFrameIndex >= 0)
                {
                    if (userMethod != null && userMethod.DeclaringType != null)
                    {
                        loggerName = userMethod.DeclaringType.FullName;
                    }
                }
            }

            ILogger logger;

            if (LogFactory != null)
            {
                logger = LogFactory.GetLogger(loggerName);
            }
            else
            {
                logger = LogManager.GetLogger(loggerName);
            }

            logLevel = _forceLogLevel ?? logLevel;
            if (!logger.IsEnabled(logLevel))
            {
                return; // We are done
            }

            var ev = new LogEventInfo();

            ev.LoggerName = loggerName;
            ev.Level      = logLevel;
            if (eventType.HasValue)
            {
                ev.Properties.Add("EventType", eventType.Value);
            }

            if (relatedActiviyId.HasValue)
            {
                ev.Properties.Add("RelatedActivityID", relatedActiviyId.Value);
            }

            ev.Message    = message;
            ev.Parameters = arguments;
            ev.Level      = _forceLogLevel ?? logLevel;

            if (eventId.HasValue)
            {
                ev.Properties.Add("EventID", eventId.Value);
            }

            if (stackTrace != null && userFrameIndex >= 0)
            {
                ev.SetStackTrace(stackTrace, userFrameIndex);
            }

            logger.Log(ev);
        }
예제 #11
0
        public void Log(LoggingData loggingData)
        {
            var logEvent = new LogEventInfo();
            logEvent.LoggerName = loggingData.LoggerName;
            logEvent.Message = loggingData.Message;
            logEvent.TimeStamp = loggingData.TimeStamp;

            if (!string.IsNullOrEmpty(loggingData.ExceptionString))
                logEvent.Exception = new Exception(loggingData.ExceptionString);

            if (loggingData.Properties != null && loggingData.Properties.Count > 0)
            {
                var properties = loggingData.Properties;
                var dict = new Dictionary<object, object>();

                foreach(var k in properties.Keys.OfType<string>())
                {
                    properties.Add(k, properties[k]);
                }
            }

            if(loggingData.LocationInfo != null)
            {
                var locationInfo = loggingData.LocationInfo;
                var lineNumber = 0;
                int.TryParse(locationInfo.LineNumber, out lineNumber);
                logEvent.SetStackTrace(new StackTrace(new StackFrame(locationInfo.FileName, lineNumber)), 0);
            }

            LogLevel level;

            if (!s_LevelDict.TryGetValue(loggingData.Level, out level))
                level = LogLevel.Info;

            logEvent.Level = level;

            m_Logger.Log(logEvent);
        }
예제 #12
0
        internal static void Write([NotNull] Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
        {
            if (targets == null)
            {
                return;
            }

            StackTraceUsage stu = targets.GetStackTraceUsage();

            if (stu != StackTraceUsage.None && !logEvent.HasStackTrace)
            {
                StackTrace stackTrace;
#if !SILVERLIGHT
                stackTrace = new StackTrace(StackTraceSkipMethods, stu == StackTraceUsage.WithSource);
#else
                stackTrace = new StackTrace();
#endif

                int firstUserFrame = FindCallingMethodOnStackTrace(stackTrace, loggerType);

                logEvent.SetStackTrace(stackTrace, firstUserFrame);
            }

            ExceptionHandlerContinuation handler;
            int numberOfTargets = 0;

            for (var t = targets; t != null; t = t.NextInChain)
            {
                FilterResult result = GetFilterResult(t.FilterChain, logEvent);

                if ((result == FilterResult.Ignore) || (result == FilterResult.IgnoreFinal))
                {
                    if (InternalLogger.IsDebugEnabled)
                    {
                        InternalLogger.Debug(
                            "{0}.{1} Rejecting message because of a filter.",
                            logEvent.LoggerName,
                            logEvent.Level);
                    }

                    if (result == FilterResult.IgnoreFinal)
                    {
                        break;
                    }
                }
                else
                {
                    numberOfTargets += 1;
                }
            }

            if (factory.Configuration.PoolingEnabled())
            {
                handler = factory.Configuration.PoolFactory.Get <ExceptionHandlerPool, ExceptionHandlerContinuation>().Get(Thread.CurrentThread.ManagedThreadId, factory.ThrowExceptions);
            }
            else
            {
                handler = new ExceptionHandlerContinuation(Thread.CurrentThread.ManagedThreadId, factory.ThrowExceptions);
            }

            bool clone = numberOfTargets > 1;

            // Clone a message for each target, so that each target can put it back into the pool when its done with it.

            for (var t = targets; t != null; t = t.NextInChain)
            {
                if (!WriteToTargetWithFilterChain(t, logEvent, handler.Delegate, clone))
                {
                    break;
                }
            }


            // No targets at all wanted this log event, so put it back into the pool
            if (numberOfTargets == 0 || clone)
            {
                logEvent.PutBack();
            }
        }
예제 #13
0
        /// <summary>
        /// Process the log event
        /// <param name="logLevel">The log level.</param>
        /// <param name="loggerName">The name of the logger.</param>
        /// <param name="message">The log message.</param>
        /// <param name="arguments">The log parameters.</param>
        /// <param name="eventId">The event id.</param>
        /// <param name="eventType">The event type.</param>
        /// <param name="relatedActiviyId">The related activity id.</param>
        /// </summary>
        protected virtual void ProcessLogEventInfo(LogLevel logLevel, string loggerName, [Localizable(false)] string message, object[] arguments, int?eventId, TraceEventType?eventType, Guid?relatedActiviyId)
        {
            loggerName = (loggerName ?? Name) ?? string.Empty;

            StackTrace stackTrace     = null;
            int        userFrameIndex = -1;

            if (AutoLoggerName)
            {
                stackTrace = new StackTrace();
                for (int i = 0; i < stackTrace.FrameCount; ++i)
                {
                    var frame = stackTrace.GetFrame(i);
                    loggerName = Internal.StackTraceUsageUtils.LookupClassNameFromStackFrame(frame);
                    if (!string.IsNullOrEmpty(loggerName))
                    {
                        userFrameIndex = i;
                        break;
                    }
                }
            }

            ILogger logger;

            if (LogFactory != null)
            {
                logger = LogFactory.GetLogger(loggerName);
            }
            else
            {
                logger = LogManager.GetLogger(loggerName);
            }

            logLevel = _forceLogLevel ?? logLevel;
            if (!logger.IsEnabled(logLevel))
            {
                return; // We are done
            }

            var ev = new LogEventInfo();

            ev.LoggerName = loggerName;
            ev.Level      = logLevel;
            if (eventType.HasValue)
            {
                ev.Properties.Add("EventType", eventType.Value);
            }

            if (relatedActiviyId.HasValue)
            {
                ev.Properties.Add("RelatedActivityID", relatedActiviyId.Value);
            }

            ev.Message    = message;
            ev.Parameters = arguments;
            ev.Level      = _forceLogLevel ?? logLevel;

            if (eventId.HasValue)
            {
                ev.Properties.Add("EventID", eventId.Value);
            }

            if (stackTrace != null && userFrameIndex >= 0)
            {
                ev.SetStackTrace(stackTrace, userFrameIndex);
            }

            logger.Log(ev);
        }