Exemplo n.º 1
0
 public void Log(NybusLogLevel level, IDictionary <string, object> state, Exception exception)
 {
     if (state.TryGetValue("message", out var msg) && msg is string message)
     {
         _logger.Log(level.ToMicrosoftLogLevel(), 0, state, exception, (s, e) => message);
     }
 }
Exemplo n.º 2
0
        public void BeginScopeConfiguredCorrectly()
        {
            using (_frameworkLogger.BeginScope(new Dictionary <string, object> {
                { "A", 1 }
            }))
                _frameworkLogger.Log(Microsoft.Extensions.Logging.LogLevel.Information, 0, new object(), null, null);

            Assert.Equal(1, _lastEvent.Properties["A"].LiteralValue());
        }
Exemplo n.º 3
0
        protected override void Write(NLog.LogEventInfo logEvent)
        {
            Microsoft.Extensions.Logging.LogLevel level;
            if (!_levelMapping.TryGetValue(logEvent.Level, out level))
            {
                level = Microsoft.Extensions.Logging.LogLevel.Information;
            }

            _logger.Log(level, 0, logEvent, logEvent.Exception, MessageFormatter);
        }
Exemplo n.º 4
0
        public static void Critical(this Microsoft.Extensions.Logging.ILogger logger, string message = "",
                                    [CallerMemberName] string memberName    = "",
                                    [CallerFilePath] string sourceFilePath  = "",
                                    [CallerLineNumber] int sourceLineNumber = 0)
        {
            var fileName   = Path.GetFileNameWithoutExtension(sourceFilePath);
            var methodName = memberName;
            var line       = sourceLineNumber;
            var msg        = $"{fileName} {line} [{methodName}] {message}";

            logger.Log(Microsoft.Extensions.Logging.LogLevel.Critical, null, msg);
        }
 public override void Log(ILogMessage message)
 {
     if ((int)message.Level >= (int)VerbosityLevel)
     {
         _logger.Log(
             logLevel: GetLogLevel(message.Level),
             eventId: 0,
             state: message,
             exception: null,
             formatter: (s, e) => s.Message);
     }
 }
Exemplo n.º 6
0
    public static void Info(this Microsoft.Extensions.Logging.ILogger logger, string message = "", LogType logType = LogType.Other,
                            object result = null, string errorMessage = "", Exception ex = null)
    {
        //var defaultEvent = new LogEventInfo(NLog.LogLevel.Info, "", message);

        //defaultEvent.Properties["logType"] = logType.GetDescription();
        //defaultEvent.Properties["result"] = result;
        //defaultEvent.Properties["errormessage"] = errorMessage;

        var logEvent = CreateLogEvent(message, logType, result, errorMessage);

        logger.Log(Microsoft.Extensions.Logging.LogLevel.Information,
        /// <summary>
        /// Converts NLog-LogEvent into Microsoft Extension Logging LogState
        /// </summary>
        /// <param name="logEvent"></param>
        protected override void Write(LogEventInfo logEvent)
        {
            var logLevel = ConvertToLogLevel(logEvent.Level);

            if (!_logger.IsEnabled(logLevel))
            {
                return;
            }

            var eventId = default(EventId);

            if (EventId != null)
            {
                var eventIdValue = RenderLogEvent(EventId, logEvent);
                if (!string.IsNullOrEmpty(eventIdValue) && int.TryParse(eventIdValue, out int eventIdParsed) && eventIdParsed != 0)
                {
                    eventId = new EventId(eventIdParsed);
                }
            }
            if (EventName != null)
            {
                var eventNameValue = RenderLogEvent(EventName, logEvent);
                if (!string.IsNullOrEmpty(eventNameValue))
                {
                    eventId = new EventId(eventId.Id, eventNameValue);
                }
            }

            var layoutMessage = RenderLogEvent(Layout, logEvent);
            IDictionary <string, object> contextProperties = null;

            if (ContextProperties.Count > 0 || IncludeMdlc || IncludeMdc || IncludeGdc)
            {
                contextProperties = GetContextProperties(logEvent);
                if (contextProperties?.Count == 0)
                {
                    contextProperties = null;
                }
            }

            _logger.Log(ConvertToLogLevel(logEvent.Level), eventId, new LogState(logEvent, layoutMessage, contextProperties), logEvent.Exception, LogStateFormatter);
        }
 public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
 {
     adaptee.Log(logLevel, eventId, state, exception, formatter);
 }
Exemplo n.º 9
0
 public void Debug(string format, params object[] args)
 {
     _logger.Log(LogLevel.Debug, _eventId, string.Format(format, args), null, null);
 }