Kp2aLog.cs: thread safe file access

This commit is contained in:
Philipp Crocoll 2013-08-03 12:31:40 +02:00
parent 28fb854fb1
commit 85e6a0e9c2

View File

@ -26,16 +26,29 @@ namespace keepass2android
{ {
private static bool? _logToFile; private static bool? _logToFile;
private static object _fileLocker = new object();
public static void Log(string message) public static void Log(string message)
{ {
Android.Util.Log.Debug("KP2A", message); Android.Util.Log.Debug("KP2A", message);
if (LogToFile) if (LogToFile)
{ {
using (var streamWriter = File.AppendText(LogFilename)) lock (_fileLocker)
{ {
string stringToLog = DateTime.Now+":"+DateTime.Now.Millisecond+ " -- " + message; try
streamWriter.WriteLine(stringToLog); {
using (var streamWriter = File.AppendText(LogFilename))
{
string stringToLog = DateTime.Now + ":" + DateTime.Now.Millisecond + " -- " + message;
streamWriter.WriteLine(stringToLog);
}
}
catch (Exception e)
{
Android.Util.Log.Debug("KP2A", "Couldn't write to log file. " + e);
}
} }
} }
} }