Beispiel #1
0
        public override void SetLogger()
        {
            var listener = new FoobarEventListener();

            listener.EventSourceCreated += (sender, args) =>
            {
                if (args.EventSource.Name == "Microsoft-Extensions-Logging")
                {
                    listener.EnableEvents(args.EventSource, EventLevel.LogAlways);
                }
            };
            listener.EventWritten += (sender, args) =>
            {
                if (args.EventName == "FormattedMessage")
                {
                    var payload         = args.Payload;
                    var payloadNames    = args.PayloadNames;
                    var indexOfLevel    = payloadNames.IndexOf("Level");
                    var indexOfCategory = args.PayloadNames.IndexOf("LoggerName");
                    var indexOfEventId  = args.PayloadNames.IndexOf("EventId");
                    var indexOfMessage  = args.PayloadNames.IndexOf("FormattedMessage");
                    Console.WriteLine($"{payload[indexOfLevel],-11}:{payload[indexOfCategory]}[{payload[indexOfEventId]}]");
                    Console.WriteLine($"{"",-13}{payload[indexOfMessage]}");
                }
            };

            _logger = LoggerFactory.Create(builder =>
            {
                /**
                 * Trace文件输出日志
                 * 需要导入Microsoft.Extensions.Logging.TraceSource包
                 * 会注册TraceSourceLoggerProvider
                 */
                builder.AddTraceSource(new SourceSwitch("default", "All"), new DefaultTraceListener {
                    LogFileName = "trace.log"
                });

                /**
                 * 事件输出日志
                 * 需要导入Microsoft.Extensions.Logging.EventSource包
                 * EventSourceLoggerProvider
                 */
                builder.AddEventSourceLogger();
            })
                      .CreateLogger <Program>();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var listener = new FoobarEventListener();

            listener.EventSourceCreated += (sender, args) =>
            {
                if (args.EventSource.Name == "Microsoft-Extensions-Logging")
                {
                    listener.EnableEvents(args.EventSource, EventLevel.LogAlways);
                }
            };

            listener.EventWritten += (sender, args) =>
            {
                if (args.EventName == "FormattedMessage")
                {
                    var payload         = args.Payload;
                    var payloadNames    = args.PayloadNames;
                    var indexOfLevel    = payloadNames.IndexOf("Level");
                    var indexOfCategory = payloadNames.IndexOf("LoggerName");
                    var indexOfEventId  = payloadNames.IndexOf("EventId");
                    var indexOfMessage  = payloadNames.IndexOf("FormattedMessage");
                    Console.WriteLine($"{(LogLevel)payload[indexOfLevel], -11} : {payload[indexOfCategory]} [{payload[indexOfEventId]}]");
                    Console.WriteLine($"{"", -13}{payload[indexOfMessage]}");
                }
            };

            var logger = new ServiceCollection()
                         .AddLogging(builder => builder.AddTraceSource(new SourceSwitch("default", "All"),
                                                                       new DefaultTraceListener {
                LogFileName = "trace.log"
            }).AddEventSourceLogger())
                         .BuildServiceProvider()
                         .GetRequiredService <ILogger <Program> >();

            var levels = (LogLevel[])Enum.GetValues(typeof(LogLevel));

            levels = levels.Where(it => it != LogLevel.None).ToArray();
            var eventId = 1;

            Array.ForEach(levels, level => logger.Log(level, eventId++, "This is a/an {0} log message", level));
        }