简体   繁体   English

在C#中创建日志

[英]Creating logs in c#

I am creating logs in C# by using object of ILog in log4net. 我正在通过使用log4net中的ILog对象在C#中创建日志。 I am passing two parameters 1) repository where it will create log file 2)name of log file but it is showing exception that directory is not defined and if I do it by just passing name of log file ,program runs successfully but I am unable to find the log file. 我传递了两个参数1)它将在其中创建日志文件的存储库2)日志文件的名称,但是它显示了未定义目录的异常,如果仅通过传递日志文件的名称来执行此操作,程序将成功运行,但无法执行查找日志文件。

Here is my code :- 这是我的代码:-

private void createLogger(string Logdirectory)
        {
            if (Directory.Exists(Logdirectory))
            {
                Log = LogManager.GetLogger( Logdirectory , LogFilename);

            }
            else
            {
                Log = LogManager.GetLogger(LogFilename);

            }
        } 

Here is console output :- 这是控制台输出:-

在此处输入图片说明 Help me find suitable way of getting logger by Ilog or by any other method except filestream 帮助我找到通过Ilog或通过除文件流以外的任何其他方法获取记录器的合适方法

Read the documentation, log4net is very configurable and well documented. 阅读文档,log4net是非常可配置的,并且文档齐全。

Documentation: https://logging.apache.org/log4net/release/manual/configuration.html 文档: https : //logging.apache.org/log4net/release/manual/configuration.html

using Com.Foo; // Import log4net classes.
using log4net;
using log4net.Config;
public class MyApp
{ // Define a static logger variable so that it references the // Logger instance named "MyApp".
  private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));
  static void Main(string[] args)
  { // Set up a simple configuration that logs on the console.
   BasicConfigurator.Configure();
   log.Info("Entering application.");
   Bar bar = new Bar();
   bar.DoIt();
   log.Info("Exiting application.");
 }
}

Take note the difference in the method for getting to log instance. 请注意,获取日志实例的方法有所不同。

  1. You're asking for an ILog for the current Type not an explicitly a filename 您正在为当前类型要求ILog,而不是明确的文件名
  2. You're telling log4net to read configuration settings from the app.config/web.config 您要告诉log4net从app.config / web.config中读取配置设置
  3. Depending on your config you may need to use the XmlConfigurator 根据您的配置,您可能需要使用XmlConfigurator

An example of the .config file is: .config文件的示例是:

<log4net> <!-- A1 is set to be a ConsoleAppender -->
 <appender name="A1" type="log4net.Appender.ConsoleAppender"> <!-- A1 uses PatternLayout -->
  <layout type="log4net.Layout.PatternLayout"> 
   <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" />
  </layout>
 </appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
 <root>
  <level value="DEBUG" />
  <appender-ref ref="A1" />
 </root>
</log4net>

There are lots of Appenders, above is a ConsoleAppender, but a DatabaseAppender exists and other types that might fit with you're need. 有很多Appender,上面是ConsoleAppender,但是存在DatabaseAppender,可能需要其他类型的Appender。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM