C# 在 Web 应用程序中配置 Log4Net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10204171/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:52:05  来源:igfitidea点击:

Configure Log4Net in web application

c#log4net

提问by Kris-I

I have this code and the config file below:

我有这个代码和下面的配置文件:

ILog log = LogManager.GetLogger(typeof(MyClass));
log.Debug("Testing");

TestProjdirectory is not created and if I create it, no TestLog.txtfile, no log ... nothing.

TestProj目录没有创建,如果我创建它,没有TestLog.txt文件,没有日志......什么都没有。

Any idea?

任何的想法?

Thanks,

谢谢,

The config file

配置文件

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\TestProj\TestLog.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
  </layout>
</appender>

<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>

采纳答案by shriek

You need to call the Configurefunction of the XmlConfigurator

你需要调用的Configure函数XmlConfigurator

log4net.Config.XmlConfigurator.Configure();

Either call before your first loggin call or in your Global.asax like this:

在您的第一次登录调用之前或在您的 Global.asax 中调用,如下所示:

protected void Application_Start(Object sender, EventArgs e) {
   log4net.Config.XmlConfigurator.Configure();
}

回答by Dirk Trilsbeek

often this is due to missing permissions. The windows account the local IIS Application Pool is running with may not have the permission to write to the applications directory. You could create a directory somewhere, give everyone permission to write in it and point your log4net config to that directory. If then a log file is created there, you can modify the permissions for your desired log directory so that the app pool can write to it.

通常这是由于缺少权限。运行本地 IIS 应用程序池的 Windows 帐户可能没有写入应用程序目录的权限。您可以在某处创建一个目录,授予每个人写入的权限,并将您的 log4net 配置指向该目录。如果在那里创建了一个日志文件,您可以修改所需日志目录的权限,以便应用程序池可以写入该目录。

Another reason could be an uninitialized log4net. In a winforms app, you usually configure log4net upon application start. In a web app, you can do this either dynamically (in your logging component, check if you can create a specific Ilog logger using its name, if not -> call configure()) or again upon application start in global.asax.cs.

另一个原因可能是未初始化的 log4net。在 winforms 应用程序中,您通常在应用程序启动时配置 log4net。在 Web 应用程序中,您可以动态执行此操作(在您的日志记录组件中,检查您是否可以使用其名称创建特定的 Ilog 记录器,如果不能 -> 调用 configure())或在 global.asax.cs 中的应用程序启动时再次执行此操作.

回答by hwcverwe

1: Add the following line into the AssemblyInfo class

1:在AssemblyInfo类中添加以下行

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

2: Make sure you don't use .Net Framework 4 Client Profileas Target Framework(I think this is OK on your side because otherwise it even wouldn't compile)

2:确保你不使用.Net Framework 4 Client Profileas Target Framework(我认为这对你来说是可以的,否则它甚至不会编译)

3: Make sure you log very early in your program. Otherwise, in some scenarios, it will not be initialized properly (read more on log4net FAQ).

3:确保你很早就在你的程序中登录。否则,在某些情况下,它将无法正确初始化(在log4net FAQ上阅读更多内容)。

So log something during application startup in the Global.asax

因此,在 Global.asax 中的应用程序启动期间记录一些内容

public class Global : System.Web.HttpApplication
{
    private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Global));
    protected void Application_Start(object sender, EventArgs e)
    {
        Log.Info("Startup application.");
    }
}

4: Make sure you have permission to create files and folders on the given path (if the folder itself also doesn't exist)

4:确保您有权在给定路径上创建文件和文件夹(如果文件夹本身也不存在)

5: The rest of your given information looks ok

5:你给定的其余信息看起来没问题

回答by keni

Another way to do this would be to add this line to the assembly info of the web application:

另一种方法是将此行添加到 Web 应用程序的程序集信息中:

// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Similar to Shriek's.

类似于尖叫的。

回答by Shakeer Hussain

I also had the similar issue. Logs were not creating.

我也有类似的问题。日志没有创建。

Please check logger attribute name should match with your LogManager.GetLogger("name")

请检查记录器属性名称是否应与您的 LogManager.GetLogger("name") 匹配

<logger name="Mylog">
      <level value="All"></level>
      <appender-ref ref="RollingLogFileAppender" />
    </logger>



private static readonly ILog Log = LogManager.GetLogger("Mylog");