简体   繁体   中英

ASP.NET Core EventLog provider

I have a project using .NET Framework ASP.NET Core 2.0, and want to implement logging to windows event log, like i read here<\/a>

public class Program
{
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddEventSourceLogger();
                logging.AddConsole();
            })
            .UseStartup<Startup>()
            .Build();
    }
logging.AddEventSourceLogger()

is for Event Tracing .

For the Event Log , you want to use:

logging.AddEventLog()

Install Microsoft.Extensions.Logging.EventLog from Nuget.

Include the Microsoft.Extensions.Logging.EventLog on the program.cs file

Then logging.AddEventLog() will be possible and consequently you will be able to achieve your goal

This works for me against net5.0:

 public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
                            {
                                logging.ClearProviders();
                                logging.AddEventLog(new EventLogSettings()
                                        {
                                            SourceName = ".NET Runtime",
                                            LogName = "Application",
                                        });
                            }
                         )
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM