public void ShouldBeAbleToFormatLogWithExtraProperties()
        {
            Dictionary<string, object> parameterValues = new Dictionary<string, object>();
            parameterValues["one"] = 1;
            parameterValues["two"] = "two";

            TraceLogEntry logEntry = new TraceLogEntry();
            logEntry.Categories.Add("General");
            logEntry.Categories.Add("Callhandler");
            logEntry.Message = "Logging call";
            logEntry.ExtendedProperties = parameterValues;
            logEntry.TypeName = GetType().Name;
            logEntry.MethodName = "SomeMethod";
            logEntry.ReturnValue = 42.ToString();

            string template =
                @"Message logged on {timestamp}{newline}{message}{newline}
Call on type {property(TypeName)} method {property(MethodName)}{newline}
Parameter values:{newline}
{dictionary({key} = {value}{newline})}{newline}
Return value: {property(ReturnValue)}{newline}";

            TextFormatter formatter = new TextFormatter(template);

            string formatted = formatter.Format(logEntry);

            Assert.IsTrue(formatted.Contains("Logging call"));
            Assert.IsTrue(
                formatted.Contains("Call on type TraceLogEntryFixture method SomeMethod\r\n"));
            Assert.IsTrue(formatted.Contains("one = 1\r\n"));
            Assert.IsTrue(formatted.Contains("two = two\r\n"));
            Assert.IsTrue(formatted.Contains("Return value: 42\r\n"));
        }
        /// <summary>
        /// Distribute a log entry to the sinks using a <see cref="LogDistributor"/>.
        /// </summary>
        /// <param name="logEntry">Message body</param>
        public void SendLog(LogEntry logEntry)
        {
            this.logDistributor.ProcessLog(logEntry);

            TextFormatter formatter = new TextFormatter(new TextFormatterData());
            string message = formatter.Format(logEntry);
            LoggingLogDistributedEvent.Fire(message);
        }
예제 #3
0
        public void LogSendingMessageException(Exception ex, string sink, LogEntry logEntry)
        {
            logger.AddMessage(Header, SR.InvalidSink(sink));

            TextFormatter formatter = new TextFormatter(new TextFormatterData());
            string formattedMessage = formatter.Format(logEntry);
            logger.AddMessage(logEntry.Category, SR.InvalidSinkMessage(formattedMessage));

            logger.WriteToLog(ex, Severity.Warning);
        }
예제 #4
0
        public void LogProcessLogException(LogEntry logEntry, Exception ex)
        {
            // distribution failed so write the event to the event log as a backup
            logger.AddMessage(Header, SR.ProcessMessageFailed);

            TextFormatter formatter = new TextFormatter(new TextFormatterData());
            string formattedMessage = formatter.Format(logEntry);
            logger.AddMessage(SR.ProcessMessageFailed2, SR.ProcessMessageFailed3(formattedMessage));

            logger.WriteToLog(ex, Severity.Error);
        }