2017-08-07 12:23:56 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2017-08-07 12:23:56 +02:00
|
|
|
|
*
|
|
|
|
|
* 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;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2017-08-07 12:23:56 +02:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Logging
|
2017-08-07 12:23:56 +02:00
|
|
|
|
{
|
2018-03-06 14:35:10 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Default implementation of <see cref="ILogContentFormatter"/>.
|
|
|
|
|
/// </summary>
|
2017-08-07 12:23:56 +02:00
|
|
|
|
public class DefaultLogFormatter : ILogContentFormatter
|
|
|
|
|
{
|
|
|
|
|
public string Format(ILogContent content)
|
|
|
|
|
{
|
2018-02-19 14:36:00 +01:00
|
|
|
|
if (content is ILogText text)
|
2017-08-07 12:23:56 +02:00
|
|
|
|
{
|
2018-02-19 14:36:00 +01:00
|
|
|
|
return text.Text;
|
2017-08-07 12:23:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-19 14:36:00 +01:00
|
|
|
|
if (content is ILogMessage message)
|
2017-08-07 12:23:56 +02:00
|
|
|
|
{
|
2018-02-19 14:36:00 +01:00
|
|
|
|
return FormatLogMessage(message);
|
2017-08-07 12:23:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new NotImplementedException($"The default formatter is not yet implemented for log content of type {content.GetType()}!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string FormatLogMessage(ILogMessage message)
|
|
|
|
|
{
|
|
|
|
|
var date = message.DateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
|
|
|
var severity = message.Severity.ToString().ToUpper();
|
2018-09-27 11:24:13 +02:00
|
|
|
|
var threadId = message.ThreadInfo.Id < 10 ? $"0{message.ThreadInfo.Id}" : message.ThreadInfo.Id.ToString();
|
|
|
|
|
var threadName = message.ThreadInfo.HasName ? ": " + message.ThreadInfo.Name : string.Empty;
|
|
|
|
|
var threadInfo = $"[{threadId}{threadName}]";
|
2017-08-07 12:23:56 +02:00
|
|
|
|
|
2018-09-27 11:24:13 +02:00
|
|
|
|
return $"{date} {threadInfo} - {severity}: {message.Message}";
|
2017-08-07 12:23:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|