I have a .net core 6.0 web application wherein i have to write logs in a custom Event Log(TestApp).
The logs are getting written but in .Net runtime source rather than the custom Event Log that I created.
Here's the event Log powershell script i executed
New-EventLog -Source TestApp -LogName Application
I have tried a couple of options that includes adding a LogName and application name attribute as mentioned below:
Configuring a custom event log for log4net Log4Net EventLogAppender Not Logging To Custom Event Log
Also have tried restarting system & Event viewer but the logs are written in .Net runtime source only.
Here's the Log4 net config file:
<?xml version="1.0" encoding="utf-8" ?>
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" debug="true">
<root>
<level value="ALL" />
<appender-ref ref="EventLog" />
</root>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="c:\temp\TestApp.log" />
<appendToFile value="true"/>
<maximumFileSize value="1000KB"/>
<maxSizeRollBackups value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger.%method [%line] - MESSAGE: %message%newline %exception%newline" />
</layout>
</appender>
<appender name="EventLog" type="log4net.Appender.EventLogAppender,log4net" >
<logName value="Application" />
<applicationName value="TestApp" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%date %level %logger.%method [%line] - MESSAGE: %message%newline %exception%newline" />
</layout>
</appender>
</log4net>
Here's log.release.config part
<?xml version="1.0" encoding="utf-8"?>
<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" debug="true" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<root>
<level value="#{LoggingLevel}#" xdt:Transform="Replace" />
</root>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="#{LogFileFolder}#\TestApp.log" xdt:Transform="Replace"/>
<appendToFile value="true"/>
<maximumFileSize value="1000KB"/>
<maxSizeRollBackups value="10"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger.%method [%line] - MESSAGE: %message%newline %exception%newline" />
</layout>
</appender>
</log4net>
Hostbuilder section for log:
public class Program
{
public static void Main(string[] args)
{
var host = CreateHostBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureAppConfiguration((context, config) =>
{
var environment = context.HostingEnvironment;
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true,
reloadOnChange: true);
Configuration = config.Build();
HostingEnvironment = context.HostingEnvironment;
})
.ConfigureLogging(logging =>
{
logging.AddLog4Net("log4net.config", true);
logging.AddEventLog(eventLogSettings => eventLogSettings.SourceName = "TestApp");
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseIISIntegration();
})
.Build();
host.Run();
}
public static IConfiguration? Configuration { get; set; }
public static IHostEnvironment? HostingEnvironment { get; set; }
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args);
}