/* * Copyright (c) 2018 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; using SafeExamBrowser.Contracts.Logging; namespace SafeExamBrowser.Core.Logging { public class LogFileWriter : ILogObserver { private static readonly object @lock = new object(); private readonly string filePath; private readonly ILogContentFormatter formatter; public LogFileWriter(ILogContentFormatter formatter, string filePath) { this.filePath = filePath; this.formatter = formatter; } public void Initialize() { var logFolder = Path.GetDirectoryName(filePath); if (!Directory.Exists(logFolder)) { Directory.CreateDirectory(logFolder); } } public void Notify(ILogContent content) { lock (@lock) { var raw = formatter.Format(content); using (var stream = new StreamWriter(filePath, true, Encoding.UTF8)) { stream.WriteLine(raw); } } } } }