Slava
Slava

Reputation: 6650

log4net is not generating log.txt file

I built a basic windows service in c# in VS2017 straight from the project template Windows Service. Built no problem. Installed no problem.

Here is the app.config with log4net section:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
        </startup>
        <log4net>
            <appender name="FileAppender" type="log4net.Appender.FileAppender">
                <file value="log.txt" />
                <appendToFile value="true" />
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
                </layout>
            </appender>
            <appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
                <mapping>
                    <level value="ERROR" />
                    <backColor value="Red, HighIntensity" />
                </mapping>
                <mapping>
                    <level value="WARN" />
                    <foreColor value="White" />
                    <backColor value="Yellow" />
                </mapping>
                <mapping>
                    <level value="INFO" />
                    <foreColor value="White" />
                </mapping>
                <mapping>
                    <level value="DEBUG" />
                    <backColor value="Green" />
                </mapping>
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
                </layout>
            </appender>
            <root>
                <level value="ALL" />
                <appender-ref ref="ConsoleAppender" />
                <appender-ref ref="FileAppender" />
            </root>
        </log4net>
    </configuration>

And I want to log some events, like here:

public partial class Service1 : ServiceBase
{
    private ILog log = LogManager.GetLogger(typeof(Service1));

    public Service1()
    {
        InitializeComponent();
        XmlConfigurator.Configure();
    }

    protected override void OnStart(string[] args)
    {
        log.Info("OnStart");
    }

    protected override void OnStop()
    {
        log.Info("OnStop");
    }
}

When I start the service, I dont see any log.txt file created in the folder where the exe resides (Debug\Bin).

Any idea why?

Upvotes: 0

Views: 627

Answers (3)

Eric Petroelje
Eric Petroelje

Reputation: 60549

Adding another answer since my previous hunch seemed to be wrong (or maybe you actually have two problems here). Assuming you posted your whole app.config file, it doesn't look like you declare the log4net config section anywhere in there:

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

Perhaps that's the problem?

Upvotes: 1

Franml32
Franml32

Reputation: 36

In my particular case, we always set the app.config file when initialize Log4Net on the Class Constructor:

 log4net.Config.XmlConfigurator.Configure(new Uri(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "YouAppConfigFile.config")));
 Log = log4net.LogManager.GetLogger("ServiceClass1");
 Log.Info("ServiceClass1 Started!!!");

Upvotes: 0

Eric Petroelje
Eric Petroelje

Reputation: 60549

I think the issue here I think is that your log variable is being initialized prior to the constructor being called. So basically you are calling LogManager.GetLogger prior to calling XmlConfigurator.Configure()

Try initializing log in the constructor after configuring log4net and see if that solves your problem.

public Service1()
{
    InitializeComponent();
    XmlConfigurator.Configure();
    log = LogManager.GetLogger(typeof(Service1));
}

Upvotes: 1

Related Questions