/// <summary> /// Initializes a global logfile using the specified target directory and file name /// using default settings. If not specified otherwise, a Logs directory is used and /// the log file's name is derived from the current <see cref="DateTime"/>. /// </summary> public static TextWriterLogOutput InitGlobalLogFile(string directory = null, string fileName = null, TextLogOutputConfig config = null) { // In case someone calls this multiple times, shut down the old one ShutdownTextLog(); try { // Open a writable stream to the desired, default or fallback log file location string loggingPath; if (!TryCreateLogStream(directory, fileName, out _textFileLogWriter, out loggingPath)) { Logs.Default.WriteWarning("Text Logfile unavailable, because no logging location was accessible."); return(null); } config.LoggingPath = loggingPath; // Create, configure and register a log output using the log stream _textFileLogOutput = new TextWriterLogOutput(_textFileLogWriter, config); Logs.AddGlobalOutput(_textFileLogOutput); return(_textFileLogOutput); } catch (Exception e) { Logs.Default.WriteWarning("Failed to create text logfile: {0}", LogFormat.Exception(e)); return(null); } }
private static void ShutdownTextLog() { if (_textFileLogOutput != null) { Logs.RemoveGlobalOutput(_textFileLogOutput); _textFileLogOutput = null; } if (_textFileLogWriter != null) { _textFileLogWriter.Dispose(); _textFileLogWriter = null; } }