C# 无法让 Log4Net 在我们的 WCF 应用程序中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/552718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get Log4Net to work in our WCF application
提问by Andrew Harmel-Law
We are trying to use Log4Net to log from our IIS 6-deployed WCF Application. We are trying to log to a file, but can't seem to get the log files to be created, let alone see the logging output in them. The pertinent pieces of out web.config are:
我们正在尝试使用 Log4Net 从我们的 IIS 6 部署的 WCF 应用程序登录。我们正在尝试登录到一个文件,但似乎无法获取要创建的日志文件,更不用说查看其中的日志输出了。out web.config 的相关部分是:
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
...
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.TraceLoggerFactoryAdapter, Common.Logging">
<arg key="level" value="INFO" />
<arg key="showLogName" value="true" />
<arg key="showDataTime" value="true" />
<arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
</factoryAdapter>
</logging>
</common>
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="c:\logs\ApplicationInfoTest.log" />
<threshold value="INFO" />
<param name="AppendToFile" value="true" />
<param name="DatePattern" value="ddMMyyyy" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="10MB" />
<param name="RollingStyle" value="Size" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="\r\n\r\n---------------------------------------------\r\n" />
<param name="Footer" value="\r\n---------------------------------------------\r\n\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
With this configuration we can see INFO level logging coming out of our application when using DebugView, but it is clear that this is from the
piece and not the
piece.
通过此配置,我们可以在使用 DebugView 时看到应用程序的 INFO 级别日志记录,但很明显,这是来自片段而不是
片段。
Is there something that we have failed to set up in web.config? Is it a permissions issue with the directory we have created for the logs to be written to?
是否有我们未能在 web.config 中设置的内容?我们为要写入的日志创建的目录是否存在权限问题?
Please point out our obvious mistake.
请指出我们明显的错误。
采纳答案by Ruben Bartelink
Use ProcessMonitor from SysInternalsto find out where permissions are being refused
使用SysInternals 的 ProcessMonitor找出权限被拒绝的地方
(Potentially you can determine the same info by attaching a debugger and trapping on exceptions, not in Just My Code)
(您可能可以通过附加调试器和捕获异常来确定相同的信息,而不是仅在我的代码中)
Are you sure that the process under which the service is running has permissions on the folder you're trying to write to?
您确定运行服务的进程对您尝试写入的文件夹具有权限吗?
回答by Alexander Preston
I have also had to add this line to the AssemblyInfo.cs file of my application in order to get log4net working.
我还必须将此行添加到我的应用程序的 AssemblyInfo.cs 文件中,以使 log4net 正常工作。
// LOG 4 net config
[assembly:log4net.Config.XmlConfigurator(Watch=true)]
回答by chris166
Do you have a configuration section configured for log4net? I didn't see that in your code snippet
您是否为 log4net 配置了配置部分?我在你的代码片段中没有看到
回答by Jaco Pretorius
I would first run the WCF service as a console application - this way you can specify the user account for the application to run as and see if the problem is with your config or with a permissions issue running the service through IIS.
我首先将 WCF 服务作为控制台应用程序运行 - 这样您就可以指定应用程序运行的用户帐户,并查看问题是与您的配置有关还是与通过 IIS 运行该服务的权限问题有关。
If you are unsure of how to run the service as a console application take a look at http://www.jacopretorius.net/2009/08/running-windows-service-inside-console.html
如果您不确定如何将服务作为控制台应用程序运行,请查看http://www.jacopretorius.net/2009/08/running-windows-service-inside-console.html
回答by User Friendly
Try XmlConfigurator.Configure()
试试 XmlConfigurator.Configure()