Datei: NDODLL/ServiceProvider/NDOConsoleLogger.cs
Last Commit (8aabfc4)
| 1 | -- File didn't exist -- |
New Commit (21ade13)
| 1 | // |
| 2 | // Copyright (c) 2002-2024 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | using System; |
| 23 | using Microsoft.Extensions.Logging; |
| 24 | |
| 25 | namespace NDO.Application |
| 26 | { |
| 27 | ····/// <summary> |
| 28 | ····/// Simple logger, which writes the messages to the console using Console.WriteLine. |
| 29 | ····/// </summary> |
| 30 | ····/// <remarks> |
| 31 | ····/// This logger is only used, if the application doesn't set up a host and doesn't call AddNdo and UseNdo. |
| 32 | ····/// If a host is used, the logging configuration of the host applies and setting LogLevel has no effect. |
| 33 | ····/// <code> |
| 34 | ····/// NDOConsoleLogger.LogLevel = LogLevel.Warning; |
| 35 | ····/// var pm = new PersistenceManager(); |
| 36 | ····/// </code> |
| 37 | ····/// </remarks> |
| 38 | ····public class NDOConsoleLogger : ILogger |
| 39 | ····{ |
| 40 | ········private readonly string categoryName; |
| 41 | |
| 42 | ········/// <summary> |
| 43 | ········/// Gets or sets the minimal log level. Messages with a lower log level are not written to the console. |
| 44 | ········/// The default is LogLevel.Information. |
| 45 | ········/// </summary> |
| 46 | ········public static LogLevel LogLevel { get; set; } = LogLevel.Information; |
| 47 | |
| 48 | ········/// <summary> |
| 49 | ········/// Constructs an NDOConsoleLogger object |
| 50 | ········/// </summary> |
| 51 | ········/// <param name="categoryName">The category name, which is written in front of each message.</param> |
| 52 | ········public NDOConsoleLogger( string categoryName ) |
| 53 | ········{ |
| 54 | ············this.categoryName = categoryName; |
| 55 | ········} |
| 56 | |
| 57 | ········/// <inheritdoc/> |
| 58 | ········public IDisposable BeginScope<TState>( TState state ) |
| 59 | ········{ |
| 60 | ············return NullScope.Instance; |
| 61 | ········} |
| 62 | |
| 63 | ········/// <inheritdoc/> |
| 64 | ········public bool IsEnabled( LogLevel logLevel ) |
| 65 | ········{ |
| 66 | ············return logLevel != LogLevel.None && logLevel >= LogLevel; |
| 67 | ········} |
| 68 | |
| 69 | ········/// <inheritdoc/> |
| 70 | ········public void Log<TState>( LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter ) |
| 71 | ········{ |
| 72 | ············if (!IsEnabled( logLevel )) |
| 73 | ················return; |
| 74 | |
| 75 | ············string message = formatter != null ? formatter( state, exception ) : state?.ToString(); |
| 76 | ············if (string.IsNullOrEmpty( message ) && exception == null) |
| 77 | ················return; |
| 78 | |
| 79 | ············Console.WriteLine( $"{logLevel}: {categoryName}: {message}" ); |
| 80 | ············if (exception != null) |
| 81 | ················Console.WriteLine( exception.ToString() ); |
| 82 | ········} |
| 83 | |
| 84 | ········private class NullScope : IDisposable |
| 85 | ········{ |
| 86 | ············public static readonly NullScope Instance = new NullScope(); |
| 87 | ············public void Dispose() |
| 88 | ············{ |
| 89 | ············} |
| 90 | ········} |
| 91 | ····} |
| 92 | |
| 93 | ····/// <summary> |
| 94 | ····/// Generic version of the NDOConsoleLogger, which uses the type name as category name. |
| 95 | ····/// </summary> |
| 96 | ····/// <typeparam name="T"></typeparam> |
| 97 | ····public class NDOConsoleLogger<T> : NDOConsoleLogger, ILogger<T> |
| 98 | ····{ |
| 99 | ········/// <summary> |
| 100 | ········/// Constructs an NDOConsoleLogger<T> object |
| 101 | ········/// </summary> |
| 102 | ········public NDOConsoleLogger() : base( typeof( T ).FullName ) |
| 103 | ········{ |
| 104 | ········} |
| 105 | ····} |
| 106 | |
| 107 | ····/// <summary> |
| 108 | ····/// ILoggerFactory implementation, which creates NDOConsoleLogger objects. |
| 109 | ····/// </summary> |
| 110 | ····internal class NDOConsoleLoggerFactory : ILoggerFactory |
| 111 | ····{ |
| 112 | ········public static readonly NDOConsoleLoggerFactory Instance = new NDOConsoleLoggerFactory(); |
| 113 | |
| 114 | ········public ILogger CreateLogger( string categoryName ) |
| 115 | ········{ |
| 116 | ············return new NDOConsoleLogger( categoryName ); |
| 117 | ········} |
| 118 | |
| 119 | ········public void AddProvider( ILoggerProvider provider ) |
| 120 | ········{ |
| 121 | ············throw new NotImplementedException(); |
| 122 | ········} |
| 123 | |
| 124 | ········public void Dispose() |
| 125 | ········{ |
| 126 | ········} |
| 127 | ····} |
| 128 | } |
| 129 |