简体   繁体   中英

no content into log with log4net when writing windows service program

I am configuration a commom.logging in windows service program.For now,it can generate the log file but no content in it.

I am using common.logging and log4net.

Here is my common.logging conf script:

        <configSections>
                <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
            <sectionGroup name="common">
                <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
            </sectionGroup>
        </configSections>
        <common>
            <logging>
                <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging">
                    <arg key="level" value="info" />
                    <arg key="showLogName" value="true" />
                    <arg key="showDataTime" value="true" />
                    <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
                </factoryAdapter>
            </logging>
        </common>
    </configuration>

and this is my log4net conf script:

<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <root>                          
            <level value="ALL"/>
            <appender-ref ref="rollingFile"/>           
        </root>
        <logger name="ApplicationInfoLog">
            <level value="ALL"/>            
            <appender-ref ref="rollingFile"/>
        </logger>

        <appender name="rollingFile" type="log4net.Appender.RollingFileAppender">
            <param name="File" value="Logs/a.txt"/>
            <param name="AppendToFile" value="true"/>
            <param name="RollingStyle" value="Date"/>           
            <param name="DatePattern" value="yyyy.MM.dd&quot;.txt&quot;"/>
            <param name="StaticLogFileName" value="true"/>
            <layout type="log4net.Layout.PatternLayout">            
                <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n"/>
            </layout>
        </appender>
    </log4net>

and this is the code who write content to the log:

namespace jsptpd
{
    public partial class jsptpdJobScheduler : ServiceBase
    {
        private readonly ILog logger;
        private IScheduler scheduler;

        public jsptpdJobScheduler()
        {
            InitializeComponent();
            logger = LogManager.GetCurrentClassLogger();
            logger.Info("dfsfsd");
            logger.Debug("dsfsgg");            
        }
    }
}

Where proberbly wrong and i can't find it,please help me thank you.

Log4Net fails silently on the premise that no logging is better than taking down the application. If your log4net configuration works as expected, say, in a console application, and doesn't work in your service (daemon), the most likely problem is some sort of permissions issue.

See my answer to the question, Log4net doesn't write to file for details on how to turn on log4net internal debugging. here's Phil Haack's take as well: http://haacked.com/archive/2006/09/26/Log4Net_Troubleshooting.aspx/

You maybe not using common.logging,you can configuration just using log4net like this:

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

And you can delete a logger just using root like this:

<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <root>                          
            <level value="ALL"/>
            <appender-ref ref="rollingFile"/>           
        </root>
        <appender name="rollingFile" type="log4net.Appender.RollingFileAppender">
            <param name="File" value="Logs/a.txt"/>
            <param name="AppendToFile" value="true"/>
            <param name="RollingStyle" value="Date"/>           
            <param name="DatePattern" value="yyyy.MM.dd&quot;.txt&quot;"/>
            <param name="StaticLogFileName" value="true"/>
            <layout type="log4net.Layout.PatternLayout">            
                <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n"/>
            </layout>
        </appender>
    </log4net>

It just using log4net and work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM