Exemplo n.º 1
0
            public bool Log(LogLevel logLevel, Func <string> messageFunc, Exception exception,
                            params object[] formatParameters)
            {
                if (!Initialized.Value)
                {
                    throw new LibLogException(ErrorInitializingProvider, s_initializeException);
                }

                if (messageFunc == null)
                {
                    return(IsLogLevelEnable(logLevel));
                }

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

                var formattedMessage =
                    LogMessageFormatter.FormatStructuredMessage(messageFunc(),
                                                                formatParameters,
                                                                out var patternMatches);

                var callerStackBoundaryType = typeof(Log4NetLogger);
                // Callsite HACK - Extract the callsite-logger-type from the messageFunc
                var methodType = messageFunc.Method.DeclaringType;

                if (methodType == typeof(LogExtensions) ||
                    methodType != null && methodType.DeclaringType == typeof(LogExtensions))
                {
                    callerStackBoundaryType = typeof(LogExtensions);
                }
                else if (methodType == typeof(LoggerExecutionWrapper) ||
                         methodType != null && methodType.DeclaringType == typeof(LoggerExecutionWrapper))
                {
                    callerStackBoundaryType = typeof(LoggerExecutionWrapper);
                }

                var translatedLevel = TranslateLevel(logLevel);

                object loggingEvent = s_createLoggingEvent(_logger, callerStackBoundaryType, translatedLevel,
                                                           formattedMessage, exception);

                PopulateProperties(loggingEvent, patternMatches, formatParameters);

                s_logDelegate(_logger, loggingEvent);

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

                messageFunc = LogMessageFormatter.SimulateStructuredLogging(messageFunc, formatParameters);

                _logWriteDelegate(ToLogMessageSeverity(logLevel), LogSystem, _skipLevel, exception, true, 0, null,
                                  _category, null, messageFunc.Invoke());

                return(true);
            }
Exemplo n.º 3
0
            public bool Log(LogLevel logLevel, Func <string> messageFunc, Exception exception,
                            params object[] formatParameters)
            {
                if (!Initialized.Value)
                {
                    throw new LibLogException(ErrorInitializingProvider, s_initializeException);
                }

                if (messageFunc == null)
                {
                    return(IsLogLevelEnable(logLevel));
                }

                if (s_logEventInfoFact != null)
                {
                    if (IsLogLevelEnable(logLevel))
                    {
                        var formatMessage = messageFunc();
                        if (!s_structuredLoggingEnabled)
                        {
                            IEnumerable <string> _;
                            formatMessage =
                                LogMessageFormatter.FormatStructuredMessage(formatMessage,
                                                                            formatParameters,
                                                                            out _);
                            formatParameters = null; // Has been formatted, no need for parameters
                        }

                        var callsiteLoggerType = typeof(NLogLogger);
                        // Callsite HACK - Extract the callsite-logger-type from the messageFunc
                        var methodType = messageFunc.Method.DeclaringType;
                        if (methodType == typeof(LogExtensions) ||
                            methodType != null && methodType.DeclaringType == typeof(LogExtensions))
                        {
                            callsiteLoggerType = typeof(LogExtensions);
                        }
                        else if (methodType == typeof(LoggerExecutionWrapper) || methodType != null &&
                                 methodType.DeclaringType == typeof(LoggerExecutionWrapper))
                        {
                            callsiteLoggerType = typeof(LoggerExecutionWrapper);
                        }
                        var nlogLevel = TranslateLevel(logLevel);
                        var nlogEvent = s_logEventInfoFact(_nameDelegate(), nlogLevel, formatMessage, formatParameters,
                                                           exception);
                        _logEventDelegate(callsiteLoggerType, nlogEvent);
                        return(true);
                    }

                    return(false);
                }

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

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

                    break;

                case LogLevel.Info:
                    if (_isInfoEnabledDelegate())
                    {
                        _infoDelegate(messageFunc());
                        return(true);
                    }

                    break;

                case LogLevel.Warn:
                    if (_isWarnEnabledDelegate())
                    {
                        _warnDelegate(messageFunc());
                        return(true);
                    }

                    break;

                case LogLevel.Error:
                    if (_isErrorEnabledDelegate())
                    {
                        _errorDelegate(messageFunc());
                        return(true);
                    }

                    break;

                case LogLevel.Fatal:
                    if (_isFatalEnabledDelegate())
                    {
                        _fatalDelegate(messageFunc());
                        return(true);
                    }

                    break;

                default:
                    if (_isTraceEnabledDelegate())
                    {
                        _traceDelegate(messageFunc());
                        return(true);
                    }

                    break;
                }

                return(false);
            }