log4net控制台应用程序在发布时无法记录日志

4
我有一个使用log4net(通过Castle Windsor)的控制台应用程序。当我调试时,所有内容都可以正常记录到控制台,但是当我发布并运行应用程序时,没有任何记录。
我将我的log4net配置放在一个单独的文件(log4net.config)中。 我认为它找不到配置文件,但这只是一个猜测。
我是一名Web开发人员,没有部署过很多控制台应用程序。我是否遗漏了什么? 我需要手动将log4net.config文件复制到exe目录吗?
我使用的是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个回答

10

这只是一个猜测,你是否将log4net.config文件的“Build Action”标记为'Content',并将其属性“Copy to Output directory”设置为“Copy always”。

这样,您就不必复制文件,而该文件被视为生成输出的“内容”,将包含在您的发布中。


我确实尝试过了,但仍然没有成功。我刚刚测试了一个普通的console.writeline,这个有效,所以我认为这肯定是log4net的配置问题。我想知道是否设置了错误的级别。但它被设置为全部。我的直觉是它甚至找不到配置文件... - autonomatt
我需要将App.config设置为“Content”和“Copy always”吗? - autonomatt
我又试了一下你的建议,Veljkoz...这次成功了 :) - autonomatt
... 而且回答我的子问题,app.config 不需要设置为 "Content"。 - autonomatt
1
将log4net.config文件的“Build Action”设置为“Content”,可以将该文件导出到发布版本中。在发布选项卡下的应用程序设置中检查“Application Files” -> “show all files”,如果看到配置文件,则会被导出。此方法适用于2019年 ,,/ - Metalhead1247

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接