日志记录不适用于WCF服务 [英] Logging does not work for the WCF service

查看:81
本文介绍了日志记录不适用于WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务层.它有3个项目, 线束项目(用于调试解决方案),Windows服务项目和具有业务逻辑的类库(BL项目).

I have a WCF service layer. It has 3 projects, Harness project (to debug the solution), Windows service Project and a class library with business logic(BL project).

我已经为BL项目配置了log4net.

I have configured log4net for the BL project as folows.

装配信息CS

// Configure log4net using the .config file 
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

log4net.config

log4net.config

<?xml version="1.0" encoding="utf-8" ?>
<log4netConfiguration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <appSettings>
    <add key="log4net.Config" value="log4net.config" />
  </appSettings>
  <log4net>   
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="C:\my_logs/my_service_logs/my_log_%date{ddMMyyyy}.log" />
      <appendToFile value="true" />      
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="30MB" />
      <datePattern value="yyyyMMdd" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n"/>
        <!--<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />-->
      </layout>
    </appender>   
    <root>
      <level value="INFO" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
</log4netConfiguration>

当我运行Harness项目时,日志记录工作得很好.线束项目会调用wcf服务,该服务会响应传入的请求和日志,而不会出现任何问题.

When I run the Harness project, logging works perfectly fine. Harness projects invokes the wcf service, which responds to incoming requests and logs without any issue.

但是,当我安装Windows服务时,以及通过网站日志调用BL时,都行不通.

But when I install the windows service, and when the BL is being invoked via the website logging does not work.

未为线束项目或Windows Service项目配置日志记录.

Logging is not configured for the harness project or the Windows Service project.

由于我一直在将log4net用于不同的项目,因此我确定我的配置正确.

As I have been using log4net for different projects I am sure my configurations are correct.

但是,当它通过Windows服务运行时,为什么不记录日志呢?这是某种安全问题吗? 对于日志文件/文件夹,我完全控制了所有用户.仍然没有记录. 请帮忙!

But why its not logging when it runs through the Windows service? Is it some sort of a security issue? For the log file/folders I gave full control to all users. Still its not logging. Please help!

推荐答案

log4net程序集属性的文档这样说:

因此,如果您使用配置属性,则必须调用log4net 以允许其读取属性.一个简单的调用 LogManager.GetLogger将导致调用程序集上的属性 进行读取和处理. 因此必须进行日志记录 在应用程序启动期间尽早调用,以及

Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

因此,您需要将程序集属性移到Windows Service中,并向Main方法中添加如下内容:

So you need to move the assembly attributes into the Windows Service and add something like this to the Main method:

 LogManager.GetLogger("This call initialises the logging system from the current assembly attributes");

这篇关于日志记录不适用于WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆