简体   繁体   English

编写Windows服务程序时,log4net的日志中没有内容

[英]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. 我正在Windows服务程序中配置commom.logging。目前,它可以生成日志文件,但其中没有内容。

I am using common.logging and log4net. 我正在使用common.logging和log4net。

Here is my common.logging conf script: 这是我的common.logging conf脚本:

        <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 conf脚本:

<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. Log4Net静默失败,前提是没有日志记录比关闭应用程序更好。 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. 如果您的log4net配置在控制台应用程序中按预期工作,而在服务(守护程序)中不工作,则最可能的问题是某种权限问题。

See my answer to the question, Log4net doesn't write to file for details on how to turn on log4net internal debugging. 有关如何打开log4net内部调试的详细信息,请参见Log4net不写入文件的问题的回答 here's Phil Haack's take as well: http://haacked.com/archive/2006/09/26/Log4Net_Troubleshooting.aspx/ 这也是Phil Haack的看法: http : //haacked.com/archive/2006/09/26/Log4Net_Troubleshooting.aspx/

You maybe not using common.logging,you can configuration just using log4net like this: 您可能不使用common.logging,可以仅使用log4net进行配置,如下所示:

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

And you can delete a logger just using root like this: 您可以使用root删除记录器,如下所示:

<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. 它仅使用log4net即可正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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