Here is a simple Logger class I've written:
public class Logger : ILogger
{
private readonly string logFilePath;
private readonly object threadLock;
public Logger(string filePath, string fileName)
{
this.logFilePath = Path.Combine(filePath, fileName);
this.threadLock = new object();
}
#region ILogger
void ILogger.Log(string message) => this.DoLog(message);
void ILogger.DeleteLog() => this.DoDelete();
#endregion
[Conditional("DEBUG")]
private void DoLog(string message)
{
//lock (this.threadLock)
//{
string dateTime = DateTime.UtcNow.ToString("O", System.Globalization.CultureInfo.InvariantCulture); // See https://stackoverflow.com/questions/1728404/date-format-yyyy-mm-ddthhmmssz
string logMessage = string.Format("{0} {1}{2}", dateTime, message, Environment.NewLine);
File.AppendAllText(this.logFilePath, logMessage);
//}
}
[Conditional("DEBUG")]
private void DoDelete()
{
//lock (this.threadLock)
//{
File.Delete(this.logFilePath);
//}
}
}
and I'd like to get File.Delete to fail before I add a lock around that.
I tried this unit test:
[Test]
public void TestMultithreadingDeleteLog()
{
Thread thread1 = new Thread(this.DoLog);
Thread thread2 = new Thread(this.DoDeleteLog);
this.logger.Log("TestMultithreadingDeleteLog");
//
thread1.Start();
thread2.Start();
//
thread1.Join(); // Blocks the current thread until thread1 completes or aborts)
thread2.Join();
}
private void DoLog()
{
for (int x = 0; x < 100; x++)
{
this.logger.Log("ABC");
}
}
private void DoDeleteLog()
{
for (int x = 0; x < 100; x++)
{
this.logger.DeleteLog();
}
}
but it always passes, even with much larger numbers.
How can I get File.Delete to throw an exception so I can then add the lock and check that fixes it?