C# 托管在IIS中的WCF服务库的log4net
对于一个项目,我有一个WCF服务库(目前非常简单) 它通过WCF服务网站项目托管在IIS 7.5中 对于那个WCF服务库,我需要log4net来记录一些重大事件 但在启动和访问网站后,不会创建日志文件 以下是我的配置详细信息:C# 托管在IIS中的WCF服务库的log4net,c#,wcf,iis,log4net,C#,Wcf,Iis,Log4net,对于一个项目,我有一个WCF服务库(目前非常简单) 它通过WCF服务网站项目托管在IIS 7.5中 对于那个WCF服务库,我需要log4net来记录一些重大事件 但在启动和访问网站后,不会创建日志文件 以下是我的配置详细信息: App.config (is containing the following) <appSettings> <add key="log4net-config-file" value="log4net.config"/> <
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!");
Web.Config is basically the same as App.Config of service library
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
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!");
Web.Config is basically the same as App.Config of service library
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
App.config(包含以下内容)
还有这个
激活的log4net内部日志记录(通常在DebugView中看到)
DebugView中没有输出,IIS配置的日志中也没有关于log4net的内容
我怎样才能找出问题所在
我可以做些什么来找出它不工作的原因?您是否告诉log4net在登录之前在哪里可以找到配置设置?我喜欢做的一件事是创建一个小的控制台程序,以证明我的配置可以与exe一起工作,我可以快速地将其放到任何地方。一旦工作正常,我将把代码迁移到实际的程序中。这里的好处是,您可以绕过IIS或其他障碍来证明您已经准备好了正确的代码,然后如果它不起作用,您可以作为罪魁祸首排除日志记录配置之外的问题
本网站对使用app.config来配置log4net的必要性进行了非常简单的解释。
您也可以在AssemblyInfo.cs中使用此选项
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
更多信息可在此处找到:问题是:
当托管在IIS中时,在IIS的当前目录中搜索log4net配置文件,在我的示例中是:
C:\Windows\System32\inetsrv
但配置文件保存在WCF服务的物理路径中
所以我在Web.config
<appSettings>
<add key="log4net_config" value="log4net.config" />
</appSettings>
非常感谢您的回答!是的,我已经为我的服务库准备了一个自托管,在那里日志记录工作非常有魅力。从问题中可以看出,我在AssemblyInfo.cs中有一个条目。但是,在IIS中托管时仍然不会创建日志文件。啊!显然,我对滚动条视而不见。很抱歉。看来你是在看临时目录的权限问题?您是否尝试写入临时目录以外的其他目录?在查找服务可能运行的IIS用户时,我遇到了我链接的问题中的一条注释,其中一条注释说,您应该避免使用XmlConfigurator。在指定AssemblyInfo属性时进行配置。好的,因此仅使用assemby:config,而不直接调用Configure。我会试试的!将尝试另一个目录也!谢谢希望你能把它弄明白。如果没有,请告诉我,我会看看是否我能想出其他的办法来尝试,现在就可以了。问题是找不到配置。因此,我必须获取物理路径并调用Configure
。看来我应该提供一个答案。谢谢你的帮助!