0

I am trying to use NLog with XUnit Testing. Working good for the most part except I want to auto delete the log whenever I run a new batch of tests. In reading the "Perfomrance Tuning Options" I thought I could do this, but I can't seem to keep it from deleting the file for each test that is run.

I programmatically set up a new logger as to not mess up the normal applicaion logging and the overcome the My thought was to turn on DeleteOldFileOnStartup, but then use KeepFileOpen and CuncurrentWrites so that all the test threads write to the same file but still have the delete work. This doesn't work!

  //setup the test log file
  var config = new NLog.Config.LoggingConfiguration();
  var logfile = new NLog.Targets.FileTarget(debugLoggerName)
  {
    FileName = MainTestDir() + testFolder + debugLoggerName + ".txt",
    Layout = "${message}",
    DeleteOldFileOnStartup = true,
    KeepFileOpen = true,
    OpenFileCacheTimeout = 120,
    ConcurrentWrites = true
  };
  config.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, logfile);
  NLog.LogManager.Configuration = config;
runfastman
  • 927
  • 2
  • 11
  • 31
  • Is it really necessary to write to file in an unit test? I think it's a better idea to let NLog write to memory if writing to log file isn't absolutely necessary. Faster, easier and more stable. And no cleanup needed :) See https://github.com/NLog/NLog/wiki/Memory-target – Julian Apr 15 '20 at 19:10
  • I don't see how would writing to memory help me. I want to run a batch of tests and see details of what went wrong with the different tests instead of just a fail. If I write this in memory from different processes, how do I get this out to see it? – runfastman Apr 15 '20 at 19:18
  • Well writing from NLog to memory gives you the same content as writing to a file. But without the file (hassle). – Julian Apr 15 '20 at 19:31
  • Yes, but without the file, I can't ever read the output. There is no master process that I have access to in the XUnit Testing to do something with the in memory data, and so it would be gone once the unit tests are done. Thanks for the thoughts, but I don't think this would help my situation. Do you know why using ConcurrentWrites doesn't work? – runfastman Apr 15 '20 at 19:53
  • You could also redirect to XUnit-ITestOutputHelper using a custom NLog-Target. See also: https://stackoverflow.com/a/56135112/193178 – Rolf Kristensen Apr 15 '20 at 20:14

0 Answers0