Beispiel #1
0
        /// <summary>
        /// Logs the message into the EventLog defined by <typeparamref name="TEventDefinition"/>
        /// </summary>
        /// <typeparam name="TEventDefinition">
        /// Must be a derived class of <see cref="LogEventBase"/>
        /// </typeparam>
        /// <param name="message">The string message to log</param>
        /// <param name="messageParams">The parameters of the message</param>
        public static void Log <TEventDefinition>(string message = null, params object[] messageParams)
            where TEventDefinition : LogEventBase, new()
        {
            // --- Prepare the message to log
            var eventClass = new TEventDefinition();

            if (!eventClass.LogNameValid)
            {
                throw new InvalidOperationException("The event does not have a valid log name");
            }
            var msg = message != null?String.Format(message, messageParams) : eventClass.Message;

            if (msg.Length > MAX_MESSAGE_LENGTH)
            {
                msg = msg.Substring(0, MAX_MESSAGE_LENGTH);
            }

            if (TraceLogger == null)
            {
                // --- Log it to the Windows Event log
                var logger = new EventLog(LogNameMapper.Map(eventClass.LogName))
                {
                    Source      = LogSourceMapper.Map(eventClass.Source),
                    MachineName = ".",
                };
                logger.WriteEntry(msg, eventClass.Type, eventClass.EventId, eventClass.CategoryId);
            }
            else
            {
                // --- Log it to the secondary log
                LogToSecondary(eventClass, msg);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Logs the message into the EventLog defined by <typeparamref name="TEventDefinition"/>
        /// </summary>
        /// <typeparam name="TEventDefinition">
        /// Must be a derived class of <see cref="LogEventBase"/>
        /// </typeparam>
        /// <param name="message">The string message to log</param>
        /// <param name="ex">Exception to log</param>
        public static void Log <TEventDefinition>(string message, Exception ex)
            where TEventDefinition : LogEventBase, new()
        {
            // --- Prepare the message to log
            var eventClass     = new TEventDefinition();
            var messageBuilder = new StringBuilder();

            messageBuilder.AppendLine(message);
            messageBuilder.AppendFormat("{0}: {1}", ex.GetType(), ex.Message);
            messageBuilder.AppendLine();
            messageBuilder.AppendLine(ex.StackTrace);
            var innerEx = ex.InnerException;

            while (innerEx != null)
            {
                messageBuilder.AppendFormat("{0}: ", ex);
                messageBuilder.AppendLine(ex.Message);
                messageBuilder.AppendLine(ex.StackTrace);
                innerEx = innerEx.InnerException;
            }
            var finalMessage = messageBuilder.ToString();

            if (finalMessage.Length > MAX_MESSAGE_LENGTH)
            {
                finalMessage = finalMessage.Substring(0, MAX_MESSAGE_LENGTH);
            }

            if (TraceLogger == null)
            {
                var logger = new EventLog(LogNameMapper.Map(eventClass.LogName))
                {
                    Source      = LogSourceMapper.Map(eventClass.Source),
                    MachineName = ".",
                };
                logger.WriteEntry(finalMessage, eventClass.Type, eventClass.EventId, eventClass.CategoryId);
            }
            else
            {
                // --- Log it to the secondary log
                LogToSecondary(eventClass, finalMessage);
            }
        }