Exemple #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);
                }

                IEnumerable <string> patternMatches;

                var formattedMessage =
                    LogMessageFormatter.FormatStructuredMessage(messageFunc(),
                                                                formatParameters,
                                                                out 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);
            }
Exemple #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);
            }
Exemple #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);
            }
Exemple #4
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)
                        {
                            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(_logger.Name, nlogLevel, formatMessage, formatParameters,
                            exception);
                        _logger.Log(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 (_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;
            }