示例#1
0
        public void AssemblyTraceLogTest()
        {
            var log      = new AssemblyTraceLog <MyEventId, AssemblyTraceLogTests>();
            var listener = log.TraceSource.Listeners.OfType <TestTraceListener>().First();

            listener.MethodCallInformation.Clear();

            log.Information(MyEventId.MyApplicationStart, "a1");
            try
            {
                throw new ApplicationException("b1");
            }
            catch (Exception ex)
            {
                log.Error(MyEventId.MyApplicationError, ex, "a{0}", 2);
            }

            Assert.AreEqual("Essential.Diagnostics.Tests", log.TraceSource.Name);

            var events = listener.MethodCallInformation;

            Assert.AreEqual("Essential.Diagnostics.Tests", events[0].Source);
            Assert.AreEqual(1100, events[0].Id);
            Assert.AreEqual(TraceEventType.Information, events[0].EventType);
            Assert.AreEqual("a1", events[0].Message);

            Assert.AreEqual(5100, events[1].Id);
            Assert.AreEqual(TraceEventType.Error, events[1].EventType);
            StringAssert.StartsWith(events[1].Message, "a2|Exception: System.ApplicationException: b1");
        }
示例#2
0
        public void AlternateExceptionFormatting()
        {
            var log = new AssemblyTraceLog <MyEventId, AssemblyTraceLogTests>();

            log.ExceptionSeparator = " ";
            log.ExceptionFormat    = "Exception={{{0}}}";
            var listener = log.TraceSource.Listeners.OfType <TestTraceListener>().First();

            listener.MethodCallInformation.Clear();

            try
            {
                throw new ApplicationException("b1");
            }
            catch (Exception ex)
            {
                log.Error(MyEventId.MyApplicationError, ex, "a{0}", 2);
            }

            var events = listener.MethodCallInformation;

            Assert.AreEqual(5100, events[0].Id);
            Assert.AreEqual(TraceEventType.Error, events[0].EventType);
            Console.WriteLine(events[0].Message);
            StringAssert.StartsWith(events[0].Message, "a2 Exception={System.ApplicationException: b1");
        }
示例#3
0
        static void Main(string[] args)
        {
            var stopwatch = Stopwatch.StartNew();

            // Basic source
            var source = new TraceSource("SeqFrameworkDiagnostics.Example.Basic");

            source.TraceEvent(TraceEventType.Information, (int)ExampleEventId.StartExample, "Hello, {0}, from .NET Framework", Environment.UserName);
            source.TraceData(TraceEventType.Information, (int)ExampleEventId.DataTrace, "Data Item", 42.80D, Guid.NewGuid());

            source.TraceEvent(TraceEventType.Verbose, 0, "a={1} b={0}", "B", "A");

            // Detailed source
            var detailedSource = new TraceSource("SeqFrameworkDiagnostics.Example.Detailed");

            detailedSource.TraceEvent(TraceEventType.Warning, (int)ExampleEventId.DetailedWarning, "Sample detailed warning");

            // Logical operation stack context (using scope extension)
            using (var requestScope = new LogicalOperationScope(source, "Request 1234", (int)ExampleEventId.BeginRequest, (int)ExampleEventId.EndRequest, "Begin request", "End request"))
            {
                using (var transactionScope = new LogicalOperationScope("Transaction 5678"))
                {
                    source.TraceEvent(TraceEventType.Information, (int)ExampleEventId.LogicalOperationStackExample, "Sample with operation stack context {0}, {1}, {2}", 42, new DateTimeOffset(1973, 5, 14, 0, 0, 0, TimeSpan.FromHours(10)), "Fnord");
                }
            }

            // Activity ID & transfers (using scope extension)
            using (var activityScope = new ActivityScope(source))
            {
                source.TraceEvent(TraceEventType.Information, (int)ExampleEventId.ActivityTransferExample, "Correlation example with activity ID transfer");
            }

            // Fluent interface extensions
            ITraceLog <ExampleEventId> log = new AssemblyTraceLog <ExampleEventId, Program>();

            try
            {
                log.Information(ExampleEventId.FluentExample, "Fluent logging API, {0}", Environment.OSVersion);
                log.Verbose("About to throw exception");
                throw new DivideByZeroException();
            }
            catch (Exception ex)
            {
                log.Error(ExampleEventId.DivideException, ex, "Ooops!");
            }
            stopwatch.Stop();
            log.Verbose("Time {0:'P''T'hh'H'mm'M'ss'.'fff'S'}", stopwatch.Elapsed);
            log.Information(ExampleEventId.EndExample, "Done");

            Console.WriteLine("Done");
            Console.ReadKey();
        }