/// <summary> /// Initialisiert den Controller /// </summary> /// <param name="logger">Service für Lognachrichten</param> /// <param name="appLoggingLevelSwitch">Klasse, welche entscheidet, was geloggt wird</param> public LogController( ILogger <HeaterDataController> logger, AppLoggingLevelSwitch appLoggingLevelSwitch) { this.logger = logger; // Wird gemacht, damit der Loglevel von den Client-Nachrichten sperat geschaltet werden kann this.clientLogger = Serilog.Log.ForContext("SourceContext", "Client"); this.appLoggingLevelSwitch = appLoggingLevelSwitch; }
/// <summary> /// Konfiguriert den Webserver /// </summary> /// <param name="appLoggingLevelSwitch">Klasse welche die Logginglevel von der Anwendung verwaltet</param> /// <param name="args">Die Argumente welche dem Webserver übergeben werden sollen</param> /// <returns></returns> public static IHostBuilder CreateHostBuilder(AppLoggingLevelSwitch appLoggingLevelSwitch, string[] args) { return(Host.CreateDefaultBuilder(args) .UseSerilog() // Überschreibt das Logging mit Serilog .UseSystemd() // Wenn unter Linux verwendet wird .UseWindowsService() // Wenn unter Windows verwendet wird .ConfigureServices((services) => { services.AddSingleton <AppLoggingLevelSwitch>(appLoggingLevelSwitch); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup <Startup>(); } )); }
/// <summary> /// Die Funktion welcher beim Programmstart aufgerufen wird /// </summary> /// <param name="args">Die Argumente welche an das Programm gegeben werden</param> public static int Main(string[] args) { var argsList = new List <string>(args); if (argsList.Contains("-h") || argsList.Contains("--help")) { Console.WriteLine("Hilfe:"); Console.WriteLine("========================"); Console.WriteLine("-h oder --help > Zeigt diese Hilfe an"); Console.WriteLine("--service-install-help > Zeigt einen Hilfetext an, wie man die Anwendung als Servcie installiert"); return(1); } else if (argsList.Contains("--service-install-help")) { var executablePath = AppDomain.CurrentDomain.BaseDirectory; var serviceHelpText = File.ReadAllText(Path.Combine(executablePath, "HotToInstallAsService.txt")); Console.WriteLine(serviceHelpText); return(2); } else { Console.WriteLine("Hilfe > -h"); } var logfilePath = System.IO.Path.Combine( System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), System.Reflection.Assembly.GetExecutingAssembly().GetName().Name ?? "", "Log.txt"); var appLoggingLevelSwitch = new AppLoggingLevelSwitch(); Log.Logger = new LoggerConfiguration() .MinimumLevel.ControlledBy(appLoggingLevelSwitch.GerneralLoggingLevelSwitch) .MinimumLevel.Override("Client", appLoggingLevelSwitch.ClientLoggingLevelSwitch) .MinimumLevel.Override("Microsoft", appLoggingLevelSwitch.MicrosoftLoggingLevelSwitch) .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.File(logfilePath, fileSizeLimitBytes: 10 * 1024 * 1024) .CreateLogger(); AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleUnhandledException); try { Log.Information("Starte den WebServer (args: {0})", args); CreateHostBuilder(appLoggingLevelSwitch, args).Build().Run(); } catch (Exception exception) { Log.Fatal(exception, "Fehler beim Starten des Servers"); } finally { Log.CloseAndFlush(); } return(0); }