當前位置: 首頁>>代碼示例>>C#>>正文


C# LogEventInfo.SetStackTrace方法代碼示例

本文整理匯總了C#中NLog.LogEventInfo.SetStackTrace方法的典型用法代碼示例。如果您正苦於以下問題:C# LogEventInfo.SetStackTrace方法的具體用法?C# LogEventInfo.SetStackTrace怎麽用?C# LogEventInfo.SetStackTrace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在NLog.LogEventInfo的用法示例。


在下文中一共展示了LogEventInfo.SetStackTrace方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Write

        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();

        }
開發者ID:fxmozart,項目名稱:NLog,代碼行數:51,代碼來源:LoggerImpl.cs

示例2: Write

        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;
            }
        }
開發者ID:tdhieu,項目名稱:openvss,代碼行數:91,代碼來源:LoggerImpl.cs

示例3: ProcessLogEventInfo

        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);
        }
開發者ID:893949088,項目名稱:NLog,代碼行數:79,代碼來源:NLogTraceListener.cs

示例4: Log

        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);
        }
開發者ID:kerryjiang,項目名稱:AnyLog,代碼行數:38,代碼來源:NLogLogger.cs


注:本文中的NLog.LogEventInfo.SetStackTrace方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。