Simply3DScan
Logger.cs
Go to the documentation of this file.
1 using System;
2 using System.Runtime.CompilerServices;
3 using System.Threading;
4 
5 namespace Logging
6 {
10  public class Logger
11  {
12  private LoggerTask loggerTask;
13  private Thread workerThread;
14 
20  public void StartLogging(string folder, string fileName)
21  {
22  if (this.loggerTask != null)
23  this.CloseLoggerTask();
24  this.loggerTask = new LoggerTask(folder, fileName);
25  this.workerThread = new Thread(this.loggerTask.DoWork) {IsBackground = true};
26  this.workerThread.Start();
27  }
28 
36  public void LogException(Exception exception, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
37  {
38  this.loggerTask?.EnqueueObject(new LoggerInfo(DateTime.Now, Severity.Exception, exception + exception.StackTrace, memberName, filePath, lineNumber));
39  }
40 
48  public void LogWarning(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
49  {
50  this.loggerTask?.EnqueueObject(new LoggerInfo(DateTime.Now, Severity.Warning, message, memberName, filePath, lineNumber));
51  }
52 
60  public void LogInfo(string message, [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
61  {
62  this.loggerTask?.EnqueueObject(new LoggerInfo(DateTime.Now, Severity.Info, message, memberName, filePath, lineNumber));
63  }
64 
68  public void Dispose()
69  {
70  this.CloseLoggerTask();
71  }
72 
73  private void CloseLoggerTask()
74  {
75  this.loggerTask.Stop();
76 
77  while (!this.loggerTask.IsStopped)
78  {
79  Thread.Sleep(20);
80  }
81  }
82  }
83 }
Severity
Severity of a log event
Definition: Severity.cs:6
void EnqueueObject(LoggerInfo loggerInfo)
Enqueues the object.
Definition: LoggerTask.cs:80
Class for holding log information
Definition: LoggerInfo.cs:8
void LogWarning(string message, [CallerMemberName] string memberName="", [CallerFilePath] string filePath="", [CallerLineNumber] int lineNumber=0)
Logs the warning.
Definition: Logger.cs:48
void StartLogging(string folder, string fileName)
Starts the logging.
Definition: Logger.cs:20
Class holfing a logger thread
Definition: Logger.cs:10
Definition: Logger.cs:5
void Stop()
Stops this instance.
Definition: LoggerTask.cs:88
bool IsStopped
Gets a value indicating whether this instance is stopped.
Definition: LoggerTask.cs:22
void DoWork()
Does the work. Events to log are filled in a concurrent queue and then read here in serial order...
Definition: LoggerTask.cs:43
Task for logging events
Definition: LoggerTask.cs:10
void LogInfo(string message, [CallerMemberName] string memberName="", [CallerFilePath] string filePath="", [CallerLineNumber] int lineNumber=0)
Logs the information.
Definition: Logger.cs:60
void Dispose()
Releases unmanaged and - optionally - managed resources.
Definition: Logger.cs:68
void LogException(Exception exception, [CallerMemberName] string memberName="", [CallerFilePath] string filePath="", [CallerLineNumber] int lineNumber=0)
Logs the exception.
Definition: Logger.cs:36