1
votes

There is some code for logging to file. I dont using 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);
   }
}

This program work without errors but it don't create log file c:\messages.log and don't write log entity. Where is the error? I don't want using application config file in my project

1

1 Answers

3
votes

There could be a couple of reasons (at least!) why you are not seeing any logging:

  1. The categories that are configured for logging are "Error" and "Debug" but when you call MyLogger.Write you are passing a category of "My Category"

  2. There could be a permission problem. Writing to the root of the drive is frequently restricted

As an aside, you should probably store the reference to LogWriterImpl as the base class LogWriter.

As another aside, instead of using the logging classes directly it is preferable to use the Fluent Configuration API which was released as part of version 5.0. It makes this type of configuration much simpler. As an example:

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

It's also more maintainable and supportable. E.g. there is less chance that there won't be breaking implementation changes like when LogWriter was made abstract as part of version 5.