Example #1
0
        public static void Write(
            string message,
            AppLogLevel level   = AppLogLevel.Trace,
            Exception exception = null)
        {
            try
            {
                // メッセージの改行コードを置換する
                message = message.Replace(Environment.NewLine, "<br />");
                message = message.Replace("\n", "<br />");
                message = message.Replace("\r", "<br />");

                defaultLogger?.Log(ConvetLogLevel(level), exception, message);

                Trace.WriteLine(
                    $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} [{level.ToString().ToUpper().PadRight(5)}] {message}");
                if (exception != null)
                {
                    Trace.WriteLine(exception.ToFormat());
                }
            }
            catch (Exception)
            {
            }
        }
Example #2
0
        /// <summary>
        /// Writes to logger.
        /// </summary>
        /// <param name="loglevel">The loglevel.</param>
        /// <param name="message">The message.</param>
        protected virtual void WriteToLogger(AppLogLevel loglevel, string message)
        {
            //used by the asf appender to log detailed or standard traces
            ThreadContext.Properties["LogLevel"] = loglevel.ToString();

            foreach (var log in _loggers)
            {
                if (loglevel == AppLogLevel.Info && log.IsInfoEnabled)                 //informational messages
                {
                    log.Info(message);
                }
                else if (loglevel == AppLogLevel.Warn && log.IsWarnEnabled)                 //warnings
                {
                    log.Warn(message);
                }
                else if (loglevel == AppLogLevel.Error && log.IsErrorEnabled)                 //handled exceptions
                {
                    log.Error(message);
                }
                else if (loglevel == AppLogLevel.Fatal && log.IsFatalEnabled)                 //un-handled exceptions, or dire exceptions
                {
                    log.Fatal(message);
                }
            }

            ThreadContext.Properties["LogLevel"] = null;
        }