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"));
        }
Ejemplo n.º 2
0
        private TraceLogEntry GetLogEntry(IMethodInvocation input)
        {
            TraceLogEntry     logEntry  = new TraceLogEntry();
            CategoryFormatter formatter = new CategoryFormatter(input.MethodBase);

            foreach (string category in this.categories)
            {
                logEntry.Categories.Add(formatter.FormatCategory(category));
            }
            logEntry.EventId  = this.eventId;
            logEntry.Priority = this.priority;
            logEntry.Severity = this.severity;
            logEntry.Title    = LogCallHandlerDefaults.Title;
            if (this.includeParameters)
            {
                Dictionary <string, object> parameters = new Dictionary <string, object>();
                for (int i = 0; i < input.Arguments.Count; i++)
                {
                    parameters[input.Arguments.GetParameterInfo(i).Name] = input.Arguments[i];
                }
                logEntry.ExtendedProperties = parameters;
            }
            if (this.includeCallStack)
            {
                logEntry.CallStack = Environment.StackTrace;
            }
            logEntry.TypeName   = input.Target.GetType().FullName;
            logEntry.MethodName = input.MethodBase.Name;
            return(logEntry);
        }
        public void ElapsedTimeShouldMatchAllTimeIfNotNull()
        {
            TraceLogEntry logEntry = new TraceLogEntry();
            var           timespan = TimeSpan.FromMinutes(5);

            logEntry.CallTime = timespan;
            Assert.AreEqual(timespan, logEntry.ElapsedTime);
        }
        public void ShouldReturnElapsedTimeZeroIfCallTimeNull()
        {
            TraceLogEntry logEntry = new TraceLogEntry();

            logEntry.CallTime = null;

            Assert.AreEqual(TimeSpan.Zero, logEntry.ElapsedTime);
        }
        public void ShouldBeCreatable()
        {
            TraceLogEntry logEntry = new TraceLogEntry();

            logEntry.TypeName    = typeof(TraceLogEntryFixture).FullName;
            logEntry.MethodName  = typeof(TraceLogEntryFixture).GetMethod("ShouldBeCreatable").Name;
            logEntry.ReturnValue = "Foo";
        }
Ejemplo n.º 6
0
 private void LogPreCall(IMethodInvocation input)
 {
     if (logBeforeCall)
     {
         TraceLogEntry logEntry = GetLogEntry(input);
         logEntry.Message = beforeMessage;
         logWriter.Write(logEntry);
     }
 }
Ejemplo n.º 7
0
 private void LogPreCall(IMethodInvocation input)
 {
     if (this.logBeforeCall)
     {
         TraceLogEntry logEntry = this.GetLogEntry(input);
         logEntry.Message = this.beforeMessage;
         logEntry.Title   = string.Format(" {0}.{1}", input.MethodBase.ReflectedType.FullName, input.MethodBase.Name);
         this.logWriter.TryWrite(logEntry.ToString());
     }
 }
Ejemplo n.º 8
0
        public void LogEntryNullSessionEx()
        {
            Assert.Throws <System.ArgumentNullException>(() => {
                ISessionFacade f = this.DumbAss;

                TraceLogEntry e = new TraceLogEntry("test message", System.Diagnostics.TraceEventType.Information);

                f.Log(e, null);
            });
        }
        public void ShouldBeAbleToAddParameterValues()
        {
            Dictionary <string, object> parameterValues = new Dictionary <string, object>();

            parameterValues["x"] = 12;
            parameterValues["y"] = 43;

            TraceLogEntry logEntry = new TraceLogEntry();

            logEntry.ExtendedProperties = parameterValues;

            Assert.AreEqual(12, logEntry.ExtendedProperties["x"]);
            Assert.AreEqual(43, logEntry.ExtendedProperties["y"]);
        }
Ejemplo n.º 10
0
 private void LogPostCall(IMethodInvocation input, Stopwatch sw, IMethodReturn result)
 {
     if (this.logAfterCall)
     {
         TraceLogEntry logEntry = this.GetLogEntry(input);
         logEntry.Message     = this.afterMessage;
         logEntry.ReturnValue = null;
         if (result.ReturnValue != null && this.includeParameters)
         {
             logEntry.ReturnValue = result.ReturnValue.ToString();
         }
         if (result.Exception != null)
         {
             logEntry.Exception = result.Exception.ToString();
         }
         if (this.includeCallTime)
         {
             logEntry.CallTime = new TimeSpan?(sw.Elapsed);
         }
         logEntry.Title = string.Format(" {0}.{1}", input.MethodBase.ReflectedType.FullName, input.MethodBase.Name);
         this.logWriter.TryWrite(logEntry.ToString());
     }
 }
Ejemplo n.º 11
0
        private void LogPostCall(IMethodInvocation input, Stopwatch sw, IMethodReturn result)
        {
            if (logAfterCall)
            {
                TraceLogEntry logEntry = GetLogEntry(input);
                logEntry.Message     = afterMessage;
                logEntry.ReturnValue = null;

                if (result.ReturnValue != null && includeParameters)
                {
                    logEntry.ReturnValue = result.ReturnValue.ToString();
                }
                if (result.Exception != null)
                {
                    logEntry.Exception = result.Exception.ToString();
                }
                if (includeCallTime)
                {
                    logEntry.CallTime = sw.Elapsed;
                }
                logWriter.Write(logEntry);
            }
        }