예제 #1
0
 public static IList <logEvent> CreateMany(
     IEnumerable <LoggingEvent> loggingEvents,
     MachineDataProvider machineDataProvider,
     Action <string, Exception> errorHandler)
 {
     return(loggingEvents.Select(@event => Create(@event, machineDataProvider, errorHandler)).ToArray());
 }
예제 #2
0
        static logEvent Create(
            LoggingEvent loggingEvent,
            MachineDataProvider machineDataProvider,
            Action <string, Exception> errorHandler)
        {
            var logEvent = new logEvent
            {
                loggerName = loggingEvent.LoggerName,
                domain     = loggingEvent.Domain,
                identity   = loggingEvent.Identity,
                threadName = loggingEvent.ThreadName,
                userName   = loggingEvent.UserName,
                timeStamp  = loggingEvent.TimeStamp.ToUniversalTime().ToString("O"),
                exception  = loggingEvent.ExceptionObject == null ? new object() : JsonSerializableException.Create(loggingEvent.ExceptionObject),
                message    = loggingEvent.RenderedMessage,
                fix        = loggingEvent.Fix.ToString(),
                hostName   = Environment.MachineName,
                level      = loggingEvent.Level == null ? null : loggingEvent.Level.DisplayName,
                machineIp  = machineDataProvider?.MachineExternalIp
            };

            try
            {
                if (logEvent.domain == log4net.Util.SystemInfo.NotAvailableText)
                {
                    logEvent.domain = Assembly.GetEntryAssembly().GetName().Name;
                }

                if (logEvent.userName == log4net.Util.SystemInfo.NotAvailableText)
                {
                    logEvent.userName = Environment.UserName;
                }
            }
            catch (Exception ex)
            {
                errorHandler("Exception occurred while adding properties to log event", ex);
            }

            // Added special handling of the MessageObject since it may be an exception.
            // Exception Types require specialized serialization to prevent serialization exceptions.
            if (loggingEvent.MessageObject != null && loggingEvent.MessageObject.GetType() != typeof(string))
            {
                if (loggingEvent.MessageObject is Exception)
                {
                    logEvent.messageObject = JsonSerializableException.Create((Exception)loggingEvent.MessageObject);
                }
                else
                {
                    logEvent.messageObject = loggingEvent.MessageObject;
                }
            }
            else
            {
                logEvent.messageObject = new object();
            }

            if (loggingEvent.LocationInformation != null)
            {
                logEvent.className  = loggingEvent.LocationInformation.ClassName;
                logEvent.fileName   = loggingEvent.LocationInformation.FileName;
                logEvent.lineNumber = loggingEvent.LocationInformation.LineNumber;
                logEvent.fullInfo   = loggingEvent.LocationInformation.FullInfo;
                logEvent.methodName = loggingEvent.LocationInformation.MethodName;
            }

            AddProperties(loggingEvent, logEvent);

            return(logEvent);
        }