1
votes

I have searched far and wide for my solution (including relevant stackoverflow questions) but have yet to find an answer that fixes my issue. I have been using log4net successfully in a work project and when I run the program in visual studio I get my log file. If I install the program using the msi installer (wix) then I do NOT get a log file. I'll first list things I've tried, followed by an edited version of my code.

Also, anywhere I say "appname" I did not actually put appname, just keeping project name out of post.

  1. I'm placing the log file in ${USERPROFILE}\AppData\Local[appname] directory, permissions are not an issue.

  2. Since the config file that is present in the installed version is appname.exe.config instead of app.config, I changed the Assemblyinfo.cs assembly statement to appname.exe.config. This however did not solve my issue, it still only works while running through visual studio, an installed version does not.

  3. I have modified product.wxs to deliver log4net.dll and log4net.xml to the install directory. (forgot to do this, app would not start so came back and added it in) still no change.

We have 5 projects within a single solution using log4net. all of the assemblyinfo.cs files have the following:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "appname.exe.config", Watch = true)]

I was using [assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config", Watch = true)] but changed this to the modified config file name that is placed in the install dir and the bin.

Inside of app.config(excuse any possible typos, my dev machine is airgapped and I cannot copy paste the code):

<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>
    <log4net>
        <root>
            <level value="ALL"/>
            <appender-ref ref="MyAppender"/>
        </root>
        <appender name="MyAppender" type="log4net.Appender.FileAppender">
            <file value="${USERPROFILE}\AppData\Local\appname\appname.log"/>
            <appendToFile value="true"/>
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %level - %message%newline"/>
            </layout>
        </appender>
    </log4net>
</configuration>

and in my c# code where I use the logger:

private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
2

2 Answers

1
votes

If your configuration is in your app.config, you can just use:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

When you specify a file, log4net expect the log4net element as xml root element. Which is not true for your app.config.

0
votes

@erike i'm not sure it was mine. So I do not have an link for you.