log4net console app not logging on publish

2019-05-11 18:24发布

问题:

I have a console app using log4net (via Castle Windsor). Everything logs fine to the console when I debug, but when I publish and run the app, nothing is logged.

I have my log4net configuration in a separate file (log4net.config). I'm thinking it's not finding the config file, but that's just a guess.

I'm a web dev and haven't deployed many console apps. Am I missing something? Do I need to manually copy the log4net.config file to the exe directory?

I'm on VS2010.

app.config:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/>
    </configSections>
    <appSettings>  
        ...
        <add key="log4net.Internal.Debug" value="false"/>
    </appSettings>
    <startup>        
    <supportedRuntime version="v2.0.50727"/></startup>
    <castle>
        <components>
            ...
        </components>
        <facilities>
            <facility id="loggingfacility" configfile="log4net.config" loggingapi="log4net" type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"/>
        </facilities>
    </castle>    
</configuration>

log4net.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <log4net>
        <root>
            <!-- Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
            <priority value="ALL" />
            <appender-ref ref="ConsoleAppender" />
        </root>

        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%d: [%-5p] %m%n" />
            </layout>
        </appender> 

        <logger name="Castle">
            <level value="INFO" />
        </logger>
    </log4net>
</configuration>

回答1:

This is a wild guess, but have you marked "Build Action" of your log4net.config file as 'Content', and set it's property "Copy to Output directory" to "Copy always".

This way you don't have to copy file, and this file is considered as 'content' of the build output and will be included in your publish.