使用记录多个客户端的帮助在单个进程log4net的多个、客户端、进程、log4net

由网友(帅癌晚期)分享简介:我选择了log4net的经过多番考虑,是我记录的应用程序,请在没有参数I chose log4Net after much considerations to be my Logging application so please no arguments on thatOK,听到的是我的问题。OK, hear i...

我选择了log4net的经过多番考虑,是我记录的应用程序,请在没有参数

I chose log4Net after much considerations to be my Logging application so please no arguments on that

OK,听到的是我的问题。

OK, hear is my problem

在我连接到多个客户端一个单一的过程 在每个客户端都作为一个字符串存储在其自己单独的线程的唯一ID

每个客户端使用相同的唯一ID可以连接多次 I got a single process connected to multiple Clients Each Client has a unique ID stored as a String in its own separate Thread

Each Client with the same unique ID can connect multiple times

我想在不同的.txt文件创建为每个客户端的日志文件。

I want to create a log file for every Client in a different .txt file.

这情景已经让我很困惑,因为我还没有任何应用程序的任何previous记录的经验都没有。

This scenario has got me confused since i also don't have any previous logging experience of any application at all.

我希望我已经做了我的情况再清楚不过:)

I hope i have made my scenario clear enough :)

推荐答案

感谢,你们所有的答案和帮助,但很多,很多,很多搜索我终于找到了答案后....不仅我可以创建多个文件,但动态文件names.Hope这可以帮助你们还有:)

Thank-you guys for all your answers and help but after lots and lots and lots of searching i finally found the answer .... not only i can create multiple Files but with dynamic file names.Hope this helps you guys as well :)

这个想法是不是基于一个配置文件,因为大家都表示,一是追加程序与一个文件名称相关联,所以人们可能能够举一个附加器动态文件名,但文件名中仍没有N多

The idea is not to be based on a config file because as everybody said, one Appender is associated with one file name so one might be able to give one appender a dynamic file name but still not N number of File names

所以我的配置如下

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

  <log4net>
  </log4net>

</configuration>

[更新]:其实你甚至不需要任何配置 是的,我的配置是空的,因为我打算创建动态配置

[UPDATE]: Actually you dont even need any config Yes, my configuration is empty, since i plan on to create dynamic configurations

下面是code:

主营:

SetLevel(clientID, "ALL");
AddAppender(clientID, CreateFileAppender(clientID, fileName));

ILog log = LogManager.GetLogger(clientID);
log.Any("whatever you want");

功能:

public static log4net.Appender.IAppender CreateFileAppender(string name, string fileName)
{
      log4net.Appender.FileAppender appender = new
      log4net.Appender.FileAppender();
      appender.Name = name;
      appender.File = fileName;
      appender.AppendToFile = false;

      log4net.Layout.PatternLayout layout = new
      log4net.Layout.PatternLayout();
      layout.ConversionPattern = "%d [%thread] %-5p %c [%a] - %m [%line] [%M]%n";
      layout.ActivateOptions();

      appender.Layout = layout;
      appender.ActivateOptions();

      return appender;
}

public static void SetLevel(string loggerName, string levelName)
{
      log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
      log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger;

      l.Level = l.Hierarchy.LevelMap[levelName];
}

// Add an appender to a logger
public static void AddAppender(string loggerName, log4net.Appender.IAppender appender)
{
      log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
      log4net.Repository.Hierarchy.Logger l=(log4net.Repository.Hierarchy.Logger)log.Logger;
      l.AddAppender(appender);
}

现在,我们要做的是创建一个具有指定名称的记录,并把它拿来无论何时何地,我们希望在任何线程或任何一类,人们甚至可以取得一个创建附加器,并修改它,如果需要使用命令

Now, what we do is create a logger with a specified name, and fetch it whenever and wherever we want in any thread or any class, one can even fetch a created appender and modify it if required by using the command

     log4net.LogManager.GetRepository().GetAppenders()

,并通过它迭代。所以,其实一切都是动态:)

and iterating through it. So actually everything is Dynamic :)

Woops忘了在一部开拓创新源添加: log4net的邮件归档

Woops forgot to add in the orignal source: Log4Net Mail archive

阅读全文

相关推荐

最新文章