Moved logging stuff over from other project.

Also updated .gitignore.
This commit is contained in:
Harrison Deng 2021-04-22 20:32:27 -05:00
parent 0e30f598c1
commit b39d8b6e85
5 changed files with 112 additions and 4 deletions

19
.gitignore vendored
View File

@ -1,4 +1,17 @@
# ---> VisualStudioCode
# Created by https://www.toptal.com/developers/gitignore/api/vscode,dotnetcore
# Edit at https://www.toptal.com/developers/gitignore?templates=vscode,dotnetcore
### DotnetCore ###
# .NET Core build folders
bin/
obj/
# Common node modules locations
/node_modules
/wwwroot/node_modules
### vscode ###
.vscode/* .vscode/*
!.vscode/settings.json !.vscode/settings.json
!.vscode/tasks.json !.vscode/tasks.json
@ -6,6 +19,4 @@
!.vscode/extensions.json !.vscode/extensions.json
*.code-workspace *.code-workspace
# Local History for Visual Studio Code # End of https://www.toptal.com/developers/gitignore/api/vscode,dotnetcore
.history/

28
ILogRecievable.cs Normal file
View File

@ -0,0 +1,28 @@
using System;
namespace SimpleLogger
{
public interface ILogReceiver
{
string Identifier { get; }
/// <summary>
/// The severity of the messages this log should receive.
/// </summary>
/// <value>The severity of the logs.</value>
LogLevel Level { get; }
/// <summary>
/// Logs the message.
/// </summary>
/// <param name="message">The message to be logged.</param>
/// <param name="time">The time at which this message was requested to be logged.</param>
/// <param name="level">The severity of this message.</param>
void LogMessage(string message, DateTime time, LogLevel level);
/// <summary>
/// Called when this receiver should explicitly flush received messages.
/// </summary>
void Flush();
}
}

10
LogLevel.cs Normal file
View File

@ -0,0 +1,10 @@
namespace SimpleLogger
{
public enum LogLevel : int
{
FATAL,
INFO,
WARNING,
DEBUG,
}
}

52
Logger.cs Normal file
View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace SimpleLogger
{
public static class Logger {
private static readonly ConcurrentDictionary<string, ILogReceiver> listeners = new ConcurrentDictionary<string, ILogReceiver>();
/// <summary>
/// Logs the message to listeners that are listening to the set severity of the message or greater.
/// </summary>
/// <param name="message">The message to log.</param>
/// <param name="level">The level of severity, by default, info.</param>
public static void Log(string message, LogLevel level = LogLevel.INFO) {
foreach (ILogReceiver listener in listeners.Values)
{
if (level <= listener.Level) {
listener.LogMessage(message, DateTime.Now, level);
}
}
}
/// <summary>
/// Adds a log listener.
/// </summary>
/// <param name="listener">The listener to add.</param>
public static void AddLogListener(ILogReceiver listener) {
listeners[listener.Identifier] = listener;
}
/// <summary>
/// Removes a log listener.
/// </summary>
/// <param name="listener">The listener to remove.</param>
public static void RemoveLogListener(ILogReceiver listener) {
ILogReceiver receiver;
listeners.TryRemove(listener.Identifier, out receiver);
}
/// <summary>
/// Called when all listeners should perform any flushing they need.
/// </summary>
public static void FlushListeners()
{
foreach (ILogReceiver listener in listeners.Values)
{
listener.Flush();
}
}
}
}

7
SimpleLogger.csproj Normal file
View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
</Project>