diff --git a/log4net-json/Appender.cs b/log4net-json/Appender.cs index 039666d..594dd9e 100644 --- a/log4net-json/Appender.cs +++ b/log4net-json/Appender.cs @@ -1,43 +1,31 @@ -using log4net.Appender; -using log4net.Core; -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using log4net.Core; +using log4net.Layout; +using Newtonsoft.Json; +using System.Numerics; namespace log4net_json { - public class CollectionAppender : AppenderSkeleton + public class CustomLayout : PatternLayout { - public static ObservableCollection logData = new ObservableCollection(); - protected override void Append(LoggingEvent loggingEvent) - { - logData.Add(new LogItem(loggingEvent)); - } - } - - public class LogItem - { - public string Logger { get; private set; } - public string Level { get; private set; } - public string Message { get; private set; } - public DateTime Timestamp { get; private set; } - public Exception ExceptionData { get; private set; } + #region Public Methods and Operators - public bool ShouldSerializeExceptionData() //This keeps things tidy when using Json.net for non exemption entries. + public override void Format(TextWriter writer, LoggingEvent loggingEvent) { - return ExceptionData != null; - } + var message = loggingEvent.MessageObject.GetType().IsPrimitive || loggingEvent.MessageObject is string || loggingEvent.MessageObject is decimal || loggingEvent.MessageObject is BigInteger + ? new { message = loggingEvent.MessageObject } + : loggingEvent.MessageObject; - public LogItem(LoggingEvent data) - { - Logger = data.LoggerName; - Level = data.Level.DisplayName; - Message = data.RenderedMessage; - Timestamp = data.TimeStamp; - ExceptionData = data.ExceptionObject; + writer.WriteLine(JsonConvert.SerializeObject(new + { + timestamp = loggingEvent.TimeStampUtc, + threadId = loggingEvent.ThreadName, + details = message, + logger = loggingEvent.LoggerName, + level = loggingEvent.Level.DisplayName, + user = loggingEvent.UserName + })); } + + #endregion } } diff --git a/log4net-json/log4net-json.csproj b/log4net-json/log4net-json.csproj index 880cfa9..b03d5d6 100644 --- a/log4net-json/log4net-json.csproj +++ b/log4net-json/log4net-json.csproj @@ -11,6 +11,7 @@ + diff --git a/log4net-json/log4net.config b/log4net-json/log4net.config index b5b2a62..4e0e42b 100644 --- a/log4net-json/log4net.config +++ b/log4net-json/log4net.config @@ -38,15 +38,36 @@ + + + + + + + + + + + + + + + + + + + + + - - + + \ No newline at end of file