Ejemplo n.º 1
0
            public bool Log(LogLevel logLevel, Func <string> messageFunc, Exception exception, params object[] formatParameters)
            {
                if (messageFunc == null)
                {
                    return(IsLogLevelEnable(logLevel));
                }

                if (!IsLogLevelEnable(logLevel))
                {
                    return(false);
                }

                string message = messageFunc();

                string formattedMessage = LogMessageFormatter.FormatStructuredMessage(
                    message,
                    formatParameters,
                    out IEnumerable <string> patternMatches);

                // determine correct caller - this might change due to jit optimizations with method inlining
                if (callerStackBoundaryType == null)
                {
                    lock (CallerStackBoundaryTypeSync)
                    {
#if !LIBLOG_PORTABLE
                        StackTrace stack    = new StackTrace();
                        Type       thisType = GetType();
                        s_callerStackBoundaryType = Type.GetType("LoggerExecutionWrapper");
                        for (int i = 1; i < stack.FrameCount; i++)
                        {
                            if (!IsInTypeHierarchy(thisType, stack.GetFrame(i).GetMethod().DeclaringType))
                            {
                                s_callerStackBoundaryType = stack.GetFrame(i - 1).GetMethod().DeclaringType;
                                break;
                            }
                        }
#else
                        callerStackBoundaryType = typeof(LoggerExecutionWrapper);
#endif
                    }
                }

                object translatedLevel = TranslateLevel(logLevel);

                object loggingEvent = createLoggingEvent(logger, callerStackBoundaryType, translatedLevel, formattedMessage, exception);

                PopulateProperties(loggingEvent, patternMatches, formatParameters);

                logDelegate(logger, loggingEvent);

                return(true);
            }
Ejemplo n.º 2
0
            public bool Log(LogLevel logLevel, Func <string> messageFunc, Exception exception, params object[] formatParameters)
            {
                int severity = MapSeverity(logLevel);

                if (messageFunc == null)
                {
                    return(shouldLog(loggerName, severity));
                }

                messageFunc = LogMessageFormatter.SimulateStructuredLogging(messageFunc, formatParameters);
                if (exception != null)
                {
                    return(LogException(logLevel, messageFunc, exception));
                }

                writeLog(loggerName, messageFunc(), severity);
                return(true);
            }
Ejemplo n.º 3
0
            public bool Log(LogLevel logLevel, Func <string> messageFunc, Exception exception, params object[] formatParameters)
            {
                if (messageFunc == null)
                {
                    // nothing to log..
                    return(true);
                }

                messageFunc = LogMessageFormatter.SimulateStructuredLogging(messageFunc, formatParameters);

                logWriteDelegate(
                    ToLogMessageSeverity(logLevel),
                    LogSystem,
                    skipLevel,
                    exception,
                    true,
                    0,
                    null,
                    category,
                    null,
                    messageFunc.Invoke());

                return(true);
            }
            public bool Log(LogLevel logLevel, Func <string> messageFunc, Exception exception, params object[] formatParameters)
            {
                if (messageFunc == null)
                {
                    return(IsLogLevelEnable(logLevel));
                }

                messageFunc = LogMessageFormatter.SimulateStructuredLogging(messageFunc, formatParameters);

                if (LogEventInfoFact != null)
                {
                    if (IsLogLevelEnable(logLevel))
                    {
                        object nlogLevel = TranslateLevel(logLevel);
                        Type   s_callerStackBoundaryType;
#if !LIBLOG_PORTABLE
                        StackTrace stack      = new StackTrace();
                        Type       thisType   = GetType();
                        Type       knownType0 = typeof(LoggerExecutionWrapper);
                        Type       knownType1 = typeof(LogExtensions);
                        //Maybe inline, so we may can't found any LibLog classes in stack
                        s_callerStackBoundaryType = null;
                        for (int i = 0; i < stack.FrameCount; i++)
                        {
                            Type declaringType = stack.GetFrame(i).GetMethod().DeclaringType;
                            if (!IsInTypeHierarchy(thisType, declaringType) &&
                                !IsInTypeHierarchy(knownType0, declaringType) &&
                                !IsInTypeHierarchy(knownType1, declaringType))
                            {
                                if (i > 1)
                                {
                                    s_callerStackBoundaryType = stack.GetFrame(i - 1).GetMethod().DeclaringType;
                                }
                                break;
                            }
                        }
#else
                        s_callerStackBoundaryType = null;
#endif
                        if (s_callerStackBoundaryType != null)
                        {
                            logger.Log(s_callerStackBoundaryType, LogEventInfoFact(logger.Name, nlogLevel, messageFunc(), exception));
                        }
                        else
                        {
                            logger.Log(LogEventInfoFact(logger.Name, nlogLevel, messageFunc(), exception));
                        }

                        return(true);
                    }

                    return(false);
                }

                if (exception != null)
                {
                    return(LogException(logLevel, messageFunc, exception));
                }

                switch (logLevel)
                {
                case LogLevel.Debug:
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug(messageFunc());
                        return(true);
                    }

                    break;

                case LogLevel.Info:
                    if (logger.IsInfoEnabled)
                    {
                        logger.Info(messageFunc());
                        return(true);
                    }

                    break;

                case LogLevel.Warn:
                    if (logger.IsWarnEnabled)
                    {
                        logger.Warn(messageFunc());
                        return(true);
                    }

                    break;

                case LogLevel.Error:
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(messageFunc());
                        return(true);
                    }

                    break;

                case LogLevel.Fatal:
                    if (logger.IsFatalEnabled)
                    {
                        logger.Fatal(messageFunc());
                        return(true);
                    }

                    break;

                default:
                    if (logger.IsTraceEnabled)
                    {
                        logger.Trace(messageFunc());
                        return(true);
                    }

                    break;
                }

                return(false);
            }