Last active
November 16, 2020 14:47
-
-
Save MiloszKrajewski/d7471e587fd8a13d31501aeaa13b236d to your computer and use it in GitHub Desktop.
Quick and dirty .NET core console application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Microsoft.Extensions.DependencyInjection | |
| Microsoft.Extensions.Logging | |
| Microsoft.Extensions.Configuration | |
| */ | |
| using System; | |
| using System.IO; | |
| using System.Reflection; | |
| using System.Threading.Tasks; | |
| using Microsoft.Extensions.Configuration; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Logging; | |
| namespace QuickApp | |
| { | |
| class Program | |
| { | |
| private static readonly Type ThisType = typeof(Program); | |
| private static readonly Assembly ThisAssembly = ThisType.Assembly; | |
| private static readonly string PackageName = ThisType.Namespace; | |
| private static readonly string ProductName = | |
| ThisAssembly.GetCustomAttribute<AssemblyProductAttribute>()?.Product; | |
| private static readonly string AssemblyPath = | |
| Path.GetDirectoryName(new Uri(ThisAssembly.CodeBase!).LocalPath); | |
| static async Task<int> Main(string[] args) | |
| { | |
| var console = new ColorConsoleProvider(); | |
| try | |
| { | |
| var loggerFactory = new LoggerFactory(); | |
| loggerFactory.AddProvider(console); | |
| var serviceCollection = new ServiceCollection(); | |
| serviceCollection.AddSingleton<ILoggerFactory>(loggerFactory); | |
| var configurationBuilder = new ConfigurationBuilder(); | |
| Configure(configurationBuilder); | |
| var configuration = configurationBuilder.Build(); | |
| serviceCollection.AddSingleton<IConfiguration>(configuration); | |
| Configure(serviceCollection, configuration); | |
| var serviceProvider = serviceCollection.BuildServiceProvider(); | |
| return await Execute(loggerFactory, serviceProvider, args); | |
| } | |
| catch (Exception e) | |
| { | |
| console.LogError(e, "Operation failed"); | |
| return -1; | |
| } | |
| } | |
| private static void Configure( | |
| ConfigurationBuilder configurationBuilder) { } | |
| private static void Configure( | |
| ServiceCollection serviceCollection, IConfiguration configuration) { } | |
| private static async Task<int> Execute( | |
| LoggerFactory loggerFactory, ServiceProvider serviceProvider, string[] args) | |
| { | |
| await Task.CompletedTask; | |
| return 0; | |
| } | |
| } | |
| internal class ColorConsoleProvider: ILoggerProvider, ILogger | |
| { | |
| public ILogger CreateLogger(string categoryName) => this; | |
| public void Log<TState>( | |
| LogLevel logLevel, EventId eventId, TState state, Exception exception, | |
| Func<TState, Exception, string> formatter) | |
| { | |
| try | |
| { | |
| Log(logLevel, formatter(state, exception)); | |
| if (exception is null) return; | |
| Log(logLevel, $"{exception.GetType().Name}: {exception.Message}"); | |
| if (string.IsNullOrWhiteSpace(exception.StackTrace)) return; | |
| Log(logLevel, exception.StackTrace); | |
| } | |
| catch (Exception e) | |
| { | |
| Log(LogLevel.Warning, $"<internal logging error: {e.GetType().Name}>"); | |
| } | |
| } | |
| private static void Log(LogLevel logLevel, string message) | |
| { | |
| if (string.IsNullOrWhiteSpace(message)) | |
| return; | |
| lock (Console.Out) | |
| { | |
| var color = Console.ForegroundColor; | |
| Console.ForegroundColor = ToColor(logLevel); | |
| Console.WriteLine(message); | |
| Console.ForegroundColor = color; | |
| } | |
| } | |
| private static ConsoleColor ToColor(LogLevel logLevel) => | |
| logLevel switch { | |
| LogLevel.Debug => ConsoleColor.Gray, | |
| LogLevel.Information => ConsoleColor.Cyan, | |
| LogLevel.Warning => ConsoleColor.Yellow, | |
| LogLevel.Error => ConsoleColor.Red, | |
| LogLevel.Critical => ConsoleColor.Magenta, | |
| _ => ConsoleColor.DarkGray | |
| }; | |
| public bool IsEnabled(LogLevel logLevel) => true; | |
| public IDisposable BeginScope<TState>(TState state) => null; | |
| public void Dispose() { } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment