Esempio n. 1
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }

            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }
            var message = formatter(state, exception);

            if (string.IsNullOrEmpty(message))
            {
                return;
            }
            if (exception != null)
            {
                message += "\n" + exception.ToString();
            }
            message = message.Length > MessageMaxLength?message.Substring(0, MessageMaxLength) : message;

            Log log = new Log
            {
                Level       = logLevel,
                Logger      = categoryName,
                Exception   = $"{exception?.Message} {exception?.InnerException?.Message} {exception?.InnerException?.InnerException?.Message}",
                Message     = message,
                EventId     = eventId.Id,
                CreatedTime = DateTime.UtcNow,
                UserId      = context?.HttpContext?.User.Id() ?? Guid.Empty,
                User        = context?.HttpContext?.User.Name()
            };

            loggingRepository.AddLog(log);
        }
 public void AddLog(LogDto log)
 {
     _repository.AddLog(new Log {
         LogType = log.LogType, Message = log.Message
     });
 }