Exemplo n.º 1
0
        static void Main()
        {
            File.AppendAllLines("log.csv", new string[] { $"EventName, Payload, ActivityId, RelatedActivityId" });
            _ = new LoggingEventListener();
            var logger = new ServiceCollection()
                         .AddLogging(builder => builder.AddEventSourceLogger())
                         .BuildServiceProvider()
                         .GetRequiredService <ILogger <Program> >();

            using (logger.BeginScope(new Dictionary <string, object> {
                ["Operation"] = "Foo"
            }))
            {
                logger.LogInformation("This is a test log written in scope 'Foo'");
                using (logger.BeginScope(new Dictionary <string, object> {
                    ["Operation"] = "Bar"
                }))
                {
                    logger.LogInformation("This is a test log written in scope 'Bar'");
                }
                using (logger.BeginScope(new Dictionary <string, object> {
                    ["Operation"] = "Baz"
                }))
                {
                    logger.LogInformation("This is a test log written in scope 'Baz'");
                }
            }
        }
Exemplo n.º 2
0
        static void Main()
        {
            _ = new LoggingEventListener();
            var logger = new ServiceCollection()
                         .AddLogging(builder => builder.AddEventSourceLogger())
                         .BuildServiceProvider()
                         .GetRequiredService <ILogger <Program> >();

            var state = new Dictionary <string, object>
            {
                ["ErrorCode"] = 100,
                ["Message"]   = "Unhandled exception"
            };

            logger.Log(LogLevel.Error, 1, state, new InvalidOperationException("This is a manually thrown exception."), (_, ex) => ex.Message);
            Console.Read();
        }