Web 服务中的 log4net 问题

发布于 2024-12-12 14:27:38 字数 2273 浏览 1 评论 0原文

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

这是 UI 层中的代码:

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 层的 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);
    }
 }

如果您有任何问题,请告诉我建议。 谢谢, 氮

We are successfully using log4net in our UI Layer but when we are testing in Webservice layer it does not work.

Here is the code in 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");

  }
}

Here is the web.config settings for the UI Layer:

<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>

Here is the code in ServiceLayer which does not work although it it is the same as above for the most:

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);
    }
 }

Please let me know if you have any suggestions.
Thanks,
N

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清秋悲枫 2024-12-19 14:27:39

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

[程序集:log4net.Config.XmlConfigurator(Watch = true)]

它有效。
谢谢。

I have just added this attribute manually in AssemblyInfo.cs:

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

It works.
Thanks.

影子的影子 2024-12-19 14:27:39

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

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

Just add below assembly in web config file of your service's project.

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
送舟行 2024-12-19 14:27:38

例如,您是否通过在 Web 服务中使用如下属性来配置 log4net:

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

Do you configure log4net for instance by having an attribute like this in your web service:

[assembly: log4net.Config.XmlConfigurator(Watch=true)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文