seb-win-refactoring/SafeExamBrowser.Core/Logging/LogFileWriter.cs

47 lines
1.1 KiB
C#
Raw Normal View History

/*
* Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System.IO;
using System.Text;
2017-07-28 14:52:15 +02:00
using SafeExamBrowser.Contracts.Configuration.Settings;
using SafeExamBrowser.Contracts.Logging;
namespace SafeExamBrowser.Core.Logging
{
public class LogFileWriter : ILogObserver
{
private static readonly object @lock = new object();
private readonly string filePath;
2017-08-07 12:23:56 +02:00
private readonly ILogContentFormatter formatter;
2017-08-07 12:23:56 +02:00
public LogFileWriter(ILogContentFormatter formatter, ISettings settings)
{
if (!Directory.Exists(settings.LogFolderPath))
{
Directory.CreateDirectory(settings.LogFolderPath);
}
2017-08-07 12:23:56 +02:00
this.filePath = settings.ApplicationLogFile;
this.formatter = formatter;
}
public void Notify(ILogContent content)
{
lock (@lock)
{
2017-08-07 12:23:56 +02:00
var raw = formatter.Format(content);
using (var stream = new StreamWriter(filePath, true, Encoding.UTF8))
{
2017-08-07 12:23:56 +02:00
stream.WriteLine(raw);
}
}
}
}
}