log4net 与 ASP.NET MVC:没有任何反应

发布于 2024-10-09 19:33:34 字数 3257 浏览 3 评论 0原文

我正在尝试将 log4net 与 ASP.NET MVC 一起使用,但我无法使用它发生任何事情。 我已经在我的网络项目根目录中创建了一个配置:

<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 程序集 [log4net,版本=1.2.10.0,文化=中性,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,版本= 1.0.0.0,文化=中性,PublicKeyToken = null]从[C:\ Users \ twaldron.BULLFROGSPAS \ AppData \ Local \ Temp \ Temporary ASP加载。 NET 文件\根\7642c99a\60feb7f2\程序集\dl3\2960c79f\b876bb2d_aca7cb01\Bullfrog.DWS.Web.DLL]

log4net:DefaultRepositorySelector:程序集 [Bullfrog.DWS.Web,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null] 未指定 RepositoryAttribute。

log4net:DefaultRepositorySelector:使用存储库 [log4net-default-repository] ​​和存储库类型 [log4net.Repository.Hierarchy.Hierarchy] 的程序集 [Bullfrog.DWS.Web,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]

log4net:DefaultRepositorySelector:使用类型[log4net.Repository.Hierarchy.Hierarchy]创建存储库[log4net-default-repository] “WebDev.WebServer20.EXE”(托管 (v2.0.50727)):已加载“匿名托管 DynamicMethods 程序集”

I am trying to use log4net with ASP.NET MVC and I cannot get anything to happen with it.
I've created a config that is in my web project root:

<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>

Before I am asked: yes, the application has permission to write to the directory. I have tested this and the application has permission to this directory.

Here is where I am trying to use 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();
    }
}

When I run the application and go to this controller log4net does nothing; it doesn't create the files in that directory or anything.

I have enabled internal debugging for log4net and I get no output errors in the console.
The following is all I see from log4net:

log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [C:\Users\twaldron.BULLFROGSPAS\AppData\Local\Temp\Temporary ASP.NET Files\root\7642c99a\60feb7f2\assembly\dl3\17247033\008dfd6d_e2d0ca01\log4net.DLL]. (.NET Runtime [2.0.50727.4952] on Microsoft Windows NT 6.1.7600.0)

log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]

log4net: DefaultRepositorySelector: Creating repository for assembly [Bullfrog.DWS.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]

log4net: DefaultRepositorySelector: Assembly [Bullfrog.DWS.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [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] does not have a RepositoryAttribute specified.

log4net: DefaultRepositorySelector: Assembly [Bullfrog.DWS.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]

log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
'WebDev.WebServer20.EXE' (Managed (v2.0.50727)): Loaded 'Anonymously Hosted DynamicMethods Assembly'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半窗疏影 2024-10-16 19:33:34

你在global.asax中有类似的东西吗?

void Application_Start(object sender, EventArgs e)
{
    string l4net = Server.MapPath("~/log4net.config");
    log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(l4net));

    ...

Do you have something like this in global.asax?

void Application_Start(object sender, EventArgs e)
{
    string l4net = Server.MapPath("~/log4net.config");
    log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(l4net));

    ...
傾旎 2024-10-16 19:33:34

确保您

log4net.Config.XmlConfigurator.Configure();

在应用程序启动时调用。

Ensure that you call

log4net.Config.XmlConfigurator.Configure();

At the startup of your application.

蛮可爱 2024-10-16 19:33:34

在 ASP.NET MVC 4 Web 应用程序中配置 Log4Net

  1. 下载 Log4Net:

  2. < p>在 ASP.Net MVC 4 应用程序中添加引用:

    1. 解决方案浏览器
    2. 右键单击项目名称
    3. 添加参考文献
    4. 浏览Log4Net.dll
  3. 修改Web.config 文件(来源):


<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>
 
  1. 修改AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
  1. 修改global.asax.cs
protected void Application_Start()
{
    log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
}
  1. 在要为其创建日志的类中添加记录器声明:
readonly log4net.ILog log =
    log4net.LogManager.GetLogger(
        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
    );
  1. 使用日志函数:
log.Error("make mistakes");

进一步阅读:http://anandsonake.blogspot.in/2013/08/log4net-in-aspnet-mvc.html

Confuguring Log4Net in ASP.NET MVC 4 Web Application

  1. Download Log4Net:

  2. Add reference in ASP.Net MVC 4 application:

    1. Solution Explorer
    2. Right click on project-name
    3. Add references
    4. Browse Log4Net.dll
  3. Modify Web.config file (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>
 
  1. Modify AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
  1. Modify global.asax.cs:
protected void Application_Start()
{
    log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
}
  1. Add logger declaration in class for which you want to make logs:
readonly log4net.ILog log =
    log4net.LogManager.GetLogger(
        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
    );
  1. Use log functions:
log.Error("make mistakes");

Further reading: http://anandsonake.blogspot.in/2013/08/log4net-in-aspnet-mvc.html

茶花眉 2024-10-16 19:33:34

在 AssemblyInfo.cs 中添加以下行:

[程序集:log4net.Config.XmlConfigurator(Watch = true)]

Add below line in AssemblyInfo.cs :

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文