我正在尝试将log4net与ASP.NET MVC一起使用,但我无法发生任何事情。 我已经在我的web项目根目录中创建了一个配置:
<log4net>
<root>
<level value="INFO" />
<appender-ref ref="RollingLogFileAppender"/>
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\DWSApplicationFiles\AppLogs\app.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\DWSApplicationFiles\AppLogs\app.log" />
<appendToFile value="false" />
<datePattern value="-dddd" />
<rollingStyle value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" />
</layout>
</appender>
</log4net>
在我被问到之前:是的,应用程序有权写入目录。我已对此进行了测试,该应用程序已获得此目录的许可。
以下是我尝试使用log4net的地方:
public class HomeController : Controller
{
readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public ActionResult Index()
{
log.Error("In Index ");
return View();
}
}
当我运行应用程序并转到此控制器时,log4net什么都不做;它不会在该目录或任何内容中创建文件。
我已经为log4net启用了内部调试,我在控制台中没有输出错误。 以下是我在log4net上看到的全部内容:
log4net:log4net assembly [log4net,Version = 1.2.10.0,Culture = neutral,PublicKeyToken = 1b44e1d426115821]。从[C:\ Users \ twaldron.BULLFROGSPAS \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 7642c99a \ 60feb7f2 \ assembly \ dl3 \ 17247033 \ 008dfd6d_e2d0ca01 \ log4net.DLL]加载。 (Microsoft Windows NT 6.1.7600.0上的.NET运行时[2.0.50727.4952])
log4net:DefaultRepositorySelector:defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net:DefaultRepositorySelector:为程序集创建存储库[Bullfrog.DWS.Web,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]
log4net:DefaultRepositorySelector:程序集[Bullfrog.DWS.Web,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]从[C:\ Users \ twaldron.BULLFROGSPAS \ AppData \ Local \ Temp \ Temporary ASP加载。 NET Files \ root \ 7642c99a \ 60feb7f2 \ assembly \ dl3 \ 2960c79f \ b876bb2d_aca7cb01 \ Bullfrog.DWS.Web.DLL]
log4net:DefaultRepositorySelector:Assembly [Bullfrog.DWS.Web,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]没有指定RepositoryAttribute。
log4net:DefaultRepositorySelector:程序集[Bullfrog.DWS.Web,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]使用repository [log4net-default-repository]和存储库类型[log4net.Repository.Hierarchy.Hierarchy]
log4net:DefaultRepositorySelector:使用类型[log4net.Repository.Hierarchy.Hierarchy]创建存储库[log4net-default-repository] 'WebDev.WebServer20.EXE'(Managed(v2.0.50727)):已加载'匿名托管的DynamicMethods程序集'
答案 0 :(得分:33)
你在global.asax中有这样的东西吗?
void Application_Start(object sender, EventArgs e)
{
string l4net = Server.MapPath("~/log4net.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(l4net));
...
答案 1 :(得分:23)
确保您致电
log4net.Config.XmlConfigurator.Configure();
在您的应用程序启动时。
答案 2 :(得分:7)
下载Log4Net:
在ASP.Net MVC 4应用程序中添加引用:
修改 Web.config 文件(source):
<configuration>
<configSections>
<section
name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="LOGS\log.txt"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<logger name="test">
<level value="DEBUG"></level>
</logger>
</log4net>
</configuration>
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
}
readonly log4net.ILog log =
log4net.LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
);
log.Error("make mistakes");
进一步阅读:http://anandsonake.blogspot.in/2013/08/log4net-in-aspnet-mvc.html
答案 3 :(得分:0)
在AssemblyInfo.cs中添加以下行:
[assembly:log4net.Config.XmlConfigurator(Watch = true)]