C# 企业库5.0:将日志写入文件而不使用XML配置

C# 企业库5.0:将日志写入文件而不使用XML配置,c#,.net,logging,enterprise-library,C#,.net,Logging,Enterprise Library,有一些用于记录到文件的代码。我不使用app.config class Program { static void Main(string[] args) { MyLogger.Write("This is message error", "My Category"); Console.ReadKey(); } } public static class MyLogger { static readonly LogWriterImpl

有一些用于记录到文件的代码。我不使用app.config

class Program
{
   static void Main(string[] args)
   {
      MyLogger.Write("This is message error", "My Category");
      Console.ReadKey();
   }      
}

public static class MyLogger
{
   static readonly LogWriterImpl _writer;

   static MyLogger()
   {
      TextFormatter formatter = new TextFormatter
            ("Timestamp: {timestamp}{newline}" +
            "Message: {message}{newline}" +
            "Category: {category}{newline}");

      var logFileListener = new Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener
      (
         "c:\\messages.log", "----------", "----------", formatter
      );


      LogSource mainLogSource = new LogSource("MainLogSource", SourceLevels.All);
      mainLogSource.Listeners.Add(logFileListener);
      LogSource nonExistantLogSource = new LogSource("Empty");

      IDictionary<string, LogSource> traceSources = new Dictionary<string, LogSource>();
      traceSources.Add("Error", mainLogSource);
      traceSources.Add("Debug", mainLogSource);


      _writer = new LogWriterImpl
      (
         new Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter[0],
         traceSources,
         nonExistantLogSource,
         nonExistantLogSource,
         mainLogSource,
         "Error",
         false,
         true
         );
   }

   public static void Write(string message)
   {
      Write(message, "Error");
   }

   public static void Write(string message, string category)
   {
      LogEntry entry = new LogEntry();

      entry.Categories.Add(category);
      entry.Message = message;

      _writer.Write(entry);
   }
}
类程序
{
静态void Main(字符串[]参数)
{
Write(“这是消息错误”,“我的类别”);
Console.ReadKey();
}      
}
公共静态类MyLogger
{
静态只读LogWriterImpl写入程序;
静态MyLogger()
{
TextFormatter formatter=新的TextFormatter
(“时间戳:{Timestamp}{newline}”+
“消息:{Message}{newline}”+
“类别:{Category}{newline}”);
var logFileListener=新的Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener
(
“c:\\messages.log”、“--------------”、“--------------”,格式化程序
);
LogSource mainLogSource=新的LogSource(“mainLogSource”,SourceLevels.All);
mainLogSource.Listeners.Add(logFileListener);
LogSource不存在LogSource=新LogSource(“空”);
IDictionary traceSources=新字典();
添加(“错误”,mainLogSource);
添加(“调试”,mainLogSource);
_writer=newlogwriterimpl
(
新的Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter[0],
追踪资源,
不存在的日志源,
不存在的日志源,
主要来源:,
“错误”,
假,,
真的
);
}
公共静态无效写入(字符串消息)
{
写(信息“错误”);
}
公共静态无效写入(字符串消息、字符串类别)
{
LogEntry entry=新的LogEntry();
条目.类别.添加(类别);
entry.Message=消息;
_编写(条目);
}
}

此程序工作正常,但不会创建日志文件c:\messages.log,也不会写入日志实体。错误在哪里?我不想在我的项目中使用应用程序配置文件

您没有看到任何日志记录可能有两个原因(至少!):

  • 为日志记录配置的类别为“错误”和“调试”,但当您调用
    MyLogger.Write
    时,您传递的类别为“我的类别”

  • 可能存在权限问题。写入驱动器根目录经常受到限制

  • 另外,您可能应该将对
    LogWriterImpl
    的引用存储为基类
    LogWriter

    另一方面,与其直接使用日志类,不如使用作为5.0版一部分发布的日志类。它使这种类型的配置更加简单。例如:

    var builder = new ConfigurationSourceBuilder();
    
    builder.ConfigureLogging()
           .WithOptions
             .DoNotRevertImpersonation()
           .LogToCategoryNamed("My Category")
             .SendTo.FlatFile("MyMessages")
               .FormatWith(new FormatterBuilder()
                 .TextFormatterNamed("Text Formatter")
                   .UsingTemplate("Timestamp: {timestamp}...{newline})}"))
                 .ToFile("c:\\messages.log");
    
    var configSource = new DictionaryConfigurationSource();
    builder.UpdateConfigurationWithReplace(configSource);
    EnterpriseLibraryContainer.Current 
      = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
    

    它也更易于维护和支持。例如,
    LogWriter
    作为第5版的一部分被抽象化时,不发生破坏性实现更改的可能性较小。

    它与以下代码配合得很好:Logger.Write(“这是错误消息”,“我的类别”);