Beispiel #1
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }
            var message = formatter(state, exception);

            if (string.IsNullOrEmpty(message))
            {
                return;
            }
            try
            {
                context.Add(new Log
                {
                    ExecutingType = typeof(T)?.ToString(), //typeof(T)?.ToString() ?? "Roman"  //typeof(T) == null ? null : typeof(T).ToString()
                    Exception     = exception?.ToString(),
                    LogLevel      = logLevel.ToString(),
                    Message       = message,
                    Stacktrace    = exception?.StackTrace,
                    Timestamp     = DateTime.Now,
                    Username      = "******" //TODO
                });
                context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #2
0
        public void Add(string message, object obj)
        {
            try
            {
                var maxMessageLength = GetMaxMessageLength();
                message = maxMessageLength != null && message.Length > maxMessageLength?message.Substring(0, (int)maxMessageLength) : message;

                context.EventLogs.Add(new EventLog {
                    Message = message, Value = obj.ToString(), CreatedTime = DateTime.UtcNow
                });
                context.SaveChanges();
                selfException = false;
            }
            catch (Exception ex)
            {
                using (var streamWriter = new StreamWriter("C:\\log\\AspCoreFileLog.txt", true))
                {
                    streamWriter.WriteLine(string.Format("Created Time: {0} | Message: {1} | Value: {2} \nException => {3}", DateTime.Now.ToString(), message, obj.ToString(), ex.ToString()));
                    streamWriter.Close();
                }
            }
        }