Exemplo n.º 1
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.º 2
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);
            }