1

I would like to obtain the date(timestamp) of my logger in a program. I configured my logger like this:

        var fileTarget = new FileTarget();
        string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string fullPath = Path.Combine(folder, "Log.txt");

        if (!File.Exists(fullPath))
        {
            File.Create(fullPath);
        }

        fileTarget.FileName = fullPath;
        config.AddTarget("file", fileTarget);

        var fileRule = new LoggingRule("*", LogLevel.Warn, fileTarget);
        config.LoggingRules.Add(fileRule);

        LogManager.Configuration = config;

In my program, I have the following:

class Program 
{
   static Logger log = LogManager.GetCurrentClassLogger();

   static void Main(string[] args)
   {
      log.Warn("Testing");
      //How can I check the logging time here?
   }
}

I can not figure out how to do it. Any help will be appreciated

user3841581
  • 2,637
  • 11
  • 47
  • 72
  • 1
    NLOG has a cool Config.XML where you can specify the format how each log entry has to look like. There you can also specify where and in which format the timestamp has to go to the log. Give me a minute to look for the tutorial – Mong Zhu Sep 27 '17 at 11:38
  • 3
    have a look at the [Layouts in the tutorial](https://github.com/NLog/NLog/wiki/Tutorial#layouts) – Mong Zhu Sep 27 '17 at 11:42
  • _"I would like to obtain the date(timestamp) of my logger in a program"_ - what date is this? When the logwas generated? When the logger was created? And why do you need to know that in your program? – stuartd Sep 27 '17 at 11:45
  • @stuartd it is the time at which that line was called. I know you can have that in the log file created but I would like to obtain it directly in my program and use it. – user3841581 Sep 27 '17 at 11:50
  • _"it is the time at which that line was called"_ - but you already know the time when you call the logger.. I'm confused. – stuartd Sep 27 '17 at 11:57

2 Answers2

3

You can get access to the timestamp of the log-event if you create the NLog.LogEventInfo yourself and pass it to the logger (instead of just the string-message)

The NLog.LogEventInfo have a property called LogEventInfo.TimeStamp

http://caraulean.com/2016/timestamp-accuracy-and-resolution-in-nlog/

Julian
  • 33,915
  • 22
  • 119
  • 174
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
2

I know you can have that in the log file created but I would like to obtain it directly in my program and use it.

then the simplest solution would be to use DateTime.Now and log the value by hand inside the code:

class Program 
{
   static Logger log = LogManager.GetCurrentClassLogger();

   static void Main(string[] args)
   {
      log.Warn(DateTime.Now.ToString("HH:mm:ss.fff MM.dd.yyyy") + " | " + "Testing");
      //How can I check the logging time here?
      // Or if you want to have it after the logger call save it afterwards
      DateTime timeStamp = DateTime.Now;
   }
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • I can then just call DateTime.Now.ToString("HH:mm:ss.fff MM.dd.yyyy") after the Warn message? – user3841581 Sep 27 '17 at 12:12
  • @user3841581 how do you intend to use the timestamp? don't you want to have it in the logfile? – Mong Zhu Sep 27 '17 at 12:28
  • @user3841581 Are you planning to measure some execution time? If so, you should have a look at [StopWatch](https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx). – Fildor Sep 27 '17 at 12:29
  • **confused** XD – Mong Zhu Sep 27 '17 at 12:31
  • :) I am suspecting that OP is trying to measure some execution time or round-trip time. If you want to do this, you'll need some start-timestamp in the beginning ... don't know why he thinks he needs NLog or any logger for that matter. – Fildor Sep 27 '17 at 12:43
  • 1
    @Fildor may be he is benchmarking the logger ?=! ;) – Mong Zhu Sep 27 '17 at 12:45
  • 1
    @user3841581 for that you use the logger, but how can a timestamp tell you "at **what** did an error occur" ? anyway. Where do you want to use the timestamp in the code? or do you want the time to end up in the logger/log-file? This is still not clear to me – Mong Zhu Sep 27 '17 at 14:06