我在让 log4net 记录我告诉它的所有内容时遇到了一些问题。看来这真的取决于我在哪里log.Info()
打电话;如果我把它放在正确的地方,它会起作用,其他时候它不会。
这是我的配置文件:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
<add key="log4net.Internal.Debug" value="true" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="rollingFileAppender" />
</root>
<appender name="rollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\binoptics\VICLogs\VICLog.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="50" />
<maximumFileSize value="50MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message - %exception - %newline" />
</layout>
</appender>
</log4net>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="ClientRoleProvider">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
</configuration>
这是调用 log.Info() 方法的客户端类:
在使用中:
using System.Reflection;
using log4net;
在我的字段声明中:
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
调用方法(这很好用):
MessageBox.Show(
"Hello " + LoginManager.CurrentUserData[1].ToString() + "!\n\n" +
"Welcome to the BinOptics Visual Inspection Console.",
"Login Successful!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
WaferTrackerWindow wtw = new WaferTrackerWindow();
wtw.Show();
log.Info(
LoginManager.CurrentUserData[1].ToString() +
" has successfully logged in. ");
return "Exit";
我以前的方式(这不起作用):
log.Info(
LoginManager.CurrentUserData[1].ToString() +
" has successfully logged in. ");
MessageBox.Show(
"Hello " + LoginManager.CurrentUserData[1].ToString() + "!\n\n" +
"Welcome to the BinOptics Visual Inspection Console.",
"Login Successful!", MessageBoxButton.OK, MessageBoxImage.Exclamation);
WaferTrackerWindow wtw = new WaferTrackerWindow();
wtw.Show();
return "Exit";
在第二个示例中,根本没有抛出异常,但 log.Info() 调用不起作用。我什至在 log.Info() 行上放了一个断点;它在那次调用中中断,一切正常,但随后我检查了我的日志文件,该行从未被写入。
在这个具体的例子中,我很幸运,因为我可以灵活地放置 log.Info() 调用;在同一个项目的其他课程中,我没有那种灵活性,并且呼叫只能在一个特定位置。
这里发生了什么?无论如何我可以解决这个问题吗?每次写入后是否需要刷新文件?任何帮助将不胜感激!!
问候,
凯尔