0

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. enter image description here

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);
}

enter image description here

  • Just modifying the settings.json is not sufficient. You probably didn't unload the original logger and register the log4net. You should give all necessary details, like your host builder and service registration – JHBonarius Jul 21 '22 at 18:27
  • I updated the question with the missing details for host builder & part of the release.config file. – Farhin Shaikh Jul 22 '22 at 05:30
  • are you using .net 5 or 6? Anyhow, you don't seem to remove any existing loggers (`builder.Logging.ClearProviders();`). And where are you registering the `log.release.config`? You shouldn't have to mention "log4net.config", as it's the default. – JHBonarius Jul 22 '22 at 07:25
  • I am using 6.0. Since this is a new application ,there aren't any existing loggers .Or am i misinterpreting your comment. – Farhin Shaikh Jul 22 '22 at 08:33
  • Well, you're not showing your `CreateHostBuilder` (you should always give a [mcve]), but you are likely calling `Host.CreateDefaultBuilder(args)`. `CreateDefaultBuilder` [will do a lot of things for you](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host#default-builder-settings), including _"Adds the following logging providers: Console, Debug, EventSource, EventLog (only when running on Windows)"_ – JHBonarius Jul 22 '22 at 08:52
  • Updated the question with the complete Program.cs file. This file & the two log4net config files as mentioned in the question are the only place that has this log4 net details. – Farhin Shaikh Jul 22 '22 at 09:09
  • You could check if "Copy to output directory" property of your configure file has been set to"Copy always"or "copy if newer" and check the comment of the answer of the link you posted – Ruikai Feng Jul 22 '22 at 19:15
  • @Ruikai Feng No change even with changing the property of the config file.As for the comments given in the link shared above,I have tried almost everything that includes log name and application name attribute to the log file but nothing works or is there any specific comment u referring to? – Farhin Shaikh Jul 22 '22 at 20:18
  • Leave away the `new Log4NetProviderOptions()`. It at the least doesn't do anything. But it might actually break other stuff – JHBonarius Jul 24 '22 at 10:42

1 Answers1

0

Finally was able to change the source to a custom App name. The AddEventLog() takes in the default .NetRuntime as the source name if not provided. Updated the program.cs file .ConfigureLogging() section along with the snap shot for reference.

.ConfigureLogging(logging =>
           {
               logging.AddLog4Net("log4net.config", true);
               logging.AddEventLog(eventLogSettings => eventLogSettings.SourceName = "TestApp");
           })