コード例 #1
0
        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);
        }
コード例 #3
0
        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"));
        }
コード例 #4
0
 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);
     }
 }
コード例 #6
0
        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);
            }
        }
コード例 #8
0
 public void ElapsedTimeShouldMatchAllTimeIfNotNull()
 {
     TraceLogEntry logEntry = new TraceLogEntry();
     var timespan = TimeSpan.FromMinutes(5);
     logEntry.CallTime = timespan;
     Assert.AreEqual(timespan, logEntry.ElapsedTime);
 }
コード例 #9
0
ファイル: LogCallHandler.cs プロジェクト: Brar/entlib
        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;
        }