Web服务中的log4net问题

时间:2011-10-27 08:49:01

标签: log4net

我们在UI层中成功使用了log4net,但是当我们在Webservice层中进行测试时,它不起作用。

以下是UI Layer中的代码:

public partial class _Default : System.Web.UI.Page 
{

  ILog logger = log4net.LogManager.GetLogger(typeof(_Default));


  protected void Page_Load(object sender, EventArgs e)
  {
      ServiceReference1.IService1 is1 = new ServiceReference1.Service1Client();
      is1.GetData(1);

      logger.Info("Hello Nine Thanks for use Log4Net,This is info message");
      logger.Debug("Hello Nine Thanks for use Log4Net,This is Debug message");
      logger.Error("Hello Nine Thanks for use Log4Net,This is Error message");
      logger.Warn("Hello Nine Thanks for use Log4Net,This is Warn message");
      logger.Fatal("Hello Nine Thanks for use Log4Net,This is Fatal message");

  }
}

以下是UI Layer的web.config设置:

<log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
  <param name="File" value="MyloggerSite2.log"/> <!-- This is logging in app root folder -->

        <param name="AppendToFile" value="true"/>
        <layout type="log4net.Layout.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-2p %c [%x] - %m%n"/>

        </layout>
    </appender>
    <root>
  <level value="All"/>
        <appender-ref ref="FileAppender"/>
    </root>
</log4net>

以下是ServiceLayer中的代码,尽管它与上面的内容完全相同但不起作用:

public class Service1 : IService1
{
    public string GetData(int value)
    {
        ILog logger = log4net.LogManager.GetLogger(typeof(Service1));

        logger.Info("Hello Nine Thanks for use Log4Net,This is info message");
        logger.Debug("Hello Nine Thanks for use Log4Net,This is Debug message");
        logger.Error("Hello Nine Thanks for use Log4Net,This is Error message");
        logger.Warn("Hello Nine Thanks for use Log4Net,This is Warn message");
        logger.Fatal("Hello Nine Thanks for use Log4Net,This is Fatal message");

        return string.Format("You entered: {0}", value);
    }
 }

如果您有任何建议,请与我们联系。 谢谢, Ñ

3 个答案:

答案 0 :(得分:3)

您是否通过在Web服务中使用这样的属性来配置log4net:

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

答案 1 :(得分:0)

我刚刚在AssemblyInfo.cs中手动添加了这个属性:

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

有效。 感谢。

答案 2 :(得分:-1)

只需在服务项目的Web配置文件中添加以下程序集即可。

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