对于一个项目,我有一个WCF服务库(目前非常简单),该库通过WCF服务网站项目托管在IIS 7.5中。
对于该WCF服务库,我需要log4net来记录一些主要事件。
但是在启动并访问该网站后,没有创建日志文件。
这是我的配置详细信息:
WCF服务库:
App.config (is containing the following)
<appSettings>
<add key="log4net-config-file" value="log4net.config"/>
<!-- <add key="log4net.Internal.Debug" value="true"/> -->
</appSettings>
log4net.config
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
</configSections>
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\Temp\\Logfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<datePattern value="yyyyMMdd"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="100KB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
</log4net>
</configuration>
In AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
On top of the Service class
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
In the Static Constructor (mainly due to fiddling around)
string configFile = ConfigurationManager.AppSettings["log4net-config-file"];
XmlConfigurator.Configure(new FileInfo(configFile));
logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
In the non-static constructor:
logger.Info("Hello world!");
Wcf服务网站项目:
Web.Config is basically the same as App.Config of service library
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
到目前为止我尝试过的是:
阅读大量的文章和做题的话题如配置log4net的为IIS承载WCF服务,这快速和肮脏的指导log4net的web应用为
activated log4net internal logging (which is normally seen in DebugView)
There is no output in DebugView and nothing about log4net in the IIS configured logs.
How can I find out what is wrong?
What can I do to find out why it is not working?
The problem was:
When hosted in IIS, the log4net config file was searched in the current directory of IIS, which is in my case:
C:\Windows\System32\inetsrv
But the config file is saved in the physical path of the WCF service.
So I had in the Web.config
<appSettings>
<add key="log4net_config" value="log4net.config" />
</appSettings>
And in the Constructor of the Service-class I call
var fullPath = ConfigurationManager.AppSettings["log4net_config"];
var physicalPath = System.Web.Hosting.HostingEnvironment.MapPath("/");
// to keep it compatible with self hosting
if (physicalPath != null)
{
fullPath = Path.Combine(physicalPath, fullPath);
}
var fileInfo = new FileInfo(fullPath);
XmlConfigurator.ConfigureAndWatch(fileInfo);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句