public void ShouldReturnElapsedTimeZeroIfCallTimeNull()
        {
            TraceLogEntry logEntry = new TraceLogEntry();
            logEntry.CallTime = null;

            Assert.AreEqual(TimeSpan.Zero, logEntry.ElapsedTime);
        }
예제 #2
0
        private TraceLogEntry GetLogEntry(IMethodInvocation input)
        {
            TraceLogEntry     logEntry  = new TraceLogEntry();
            CategoryFormatter formatter = new CategoryFormatter(input.MethodBase);

            foreach (string category in categories)
            {
                logEntry.Categories.Add(formatter.FormatCategory(category));
            }

            logEntry.EventId  = eventId;
            logEntry.Priority = priority;
            logEntry.Severity = severity;
            logEntry.Title    = LogCallHandlerDefaults.Title;

            if (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 (includeCallStack)
            {
                logEntry.CallStack = Environment.StackTrace;
            }

            logEntry.TypeName   = input.Target.GetType().FullName;
            logEntry.MethodName = input.MethodBase.Name;
            return(logEntry);
        }
        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"));
        }
 public void ShouldBeCreatable()
 {
     TraceLogEntry logEntry = new TraceLogEntry();
     logEntry.TypeName = typeof(TraceLogEntryFixture).FullName;
     logEntry.MethodName = typeof(TraceLogEntryFixture).GetMethod("ShouldBeCreatable").Name;
     logEntry.ReturnValue = "Foo";
 }
예제 #5
0
 private void LogPreCall(IMethodInvocation input)
 {
     if (logBeforeCall)
     {
         TraceLogEntry logEntry = GetLogEntry(input);
         logEntry.Message = beforeMessage;
         logWriter.Write(logEntry);
     }
 }
        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"]);
        }
예제 #7
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);
            }
        }
 public void ElapsedTimeShouldMatchAllTimeIfNotNull()
 {
     TraceLogEntry logEntry = new TraceLogEntry();
     var timespan = TimeSpan.FromMinutes(5);
     logEntry.CallTime = timespan;
     Assert.AreEqual(timespan, logEntry.ElapsedTime);
 }
예제 #9
0
        private TraceLogEntry GetLogEntry(IMethodInvocation input)
        {
            TraceLogEntry logEntry = new TraceLogEntry();
            CategoryFormatter formatter = new CategoryFormatter(input.MethodBase);
            foreach (string category in categories)
            {
                logEntry.Categories.Add(formatter.FormatCategory(category));
            }

            logEntry.EventId = eventId;
            logEntry.Priority = priority;
            logEntry.Severity = severity;
            logEntry.Title = LogCallHandlerDefaults.Title;

            if (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 (includeCallStack)
            {
                logEntry.CallStack = Environment.StackTrace;
            }

            logEntry.TypeName = input.Target.GetType().FullName;
            logEntry.MethodName = input.MethodBase.Name;
            return logEntry;
        }