/// <summary> /// Instantiate the object to enable either file logging or console logging. /// </summary> /// <param name="filename">Filename.</param> /// <param name="enableConsole">Enable or disable console logging.</param> public LoggingModule( string filename, bool enableConsole) { if (String.IsNullOrEmpty(filename) && !enableConsole) { throw new ArgumentException("Either a filename must be specified or console logging must be enabled."); } ServerIp = "127.0.0.1"; ServerPort = 514; _UDP = null; _Hostname = null; StackTrace st = new StackTrace(); _BaseDepth = st.FrameCount - 1; if (!String.IsNullOrEmpty(filename)) { LogFilename = filename; FileLogging = FileLoggingMode.SingleLogFile; } ConsoleEnable = enableConsole; }
/// <summary> /// Instantiate the object to enable either file logging or console logging. /// </summary> /// <param name="filename">Filename.</param> /// <param name="fileLoggingMode">File logging mode. If you specify 'FileWithDate', .yyyyMMdd will be appended to the specified filename.</param> /// <param name="enableConsole">Enable or disable console logging.</param> public LoggingModule( string filename, FileLoggingMode fileLoggingMode, bool enableConsole) { if (String.IsNullOrEmpty(filename)) { throw new ArgumentNullException(nameof(filename)); } ServerIp = "127.0.0.1"; ServerPort = 514; _UDP = null; _Hostname = null; StackTrace st = new StackTrace(); _BaseDepth = st.FrameCount - 1; LogFilename = filename; FileLogging = fileLoggingMode; ConsoleEnable = enableConsole; }
public void ConfigureW_WithConfiguration_AppliesFileLoggingMode(string setting, FileLoggingMode expectedMode) { var settings = new Dictionary <string, string> { { ConfigurationPath.Combine(ConfigurationSectionNames.JobHost, ConfigurationSectionNames.Logging, "fileLoggingMode"), setting } }; var options = GetConfiguredOptions(settings); Assert.Equal(expectedMode, options.FileLoggingMode); }
/// <summary> /// Creates a new syslog logger /// </summary> /// <param name="ip">IPv4 address of the syslog host</param> /// <param name="port">Port of syslog server</param> /// <param name="appName">Name of the app to show in syslog</param> /// <param name="consoleEnable">Display logs in the console</param> /// <param name="fileLog">Output logs to file. Default: disabled</param> /// <param name="logFileName">Output file name (if enabled). The extension of '.log' will always be appended. Defaults to the current exe name (ex. app.exe.log).</param> /// <param name="overwrite">If a log file with the same name exists, should we overwrite it.</param> public static void CreateLogger(string ip, int port, string appName, bool consoleEnable = true, FileLoggingMode fileLog = FileLoggingMode.Disabled, string logFileName = null, bool overwrite = true) { //set some defaults for displaying logs in console _logger.ConsoleEnable = consoleEnable; _logger.EnableColors = true; //make sure this is a valid IPv4 address & create the logger if it is if (System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) { _logger = new LoggingModule(ip, port); } else { throw new Exception("Invalid IPv4 address"); } //hold app name to filename character requirements if (appName.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) == -1) { _appName = appName; } else { throw new Exception("App name includes invalid characters."); } if (fileLog != FileLoggingMode.Disabled) { //if logFileName is the default, set it to <appname.exe>, we will append ".log" later if (logFileName == null) { logFileName = Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); } //make sure the filename isn't empty if (logFileName.Length > 0) { //check if the filename or path contain any invalid characters if (logFileName.IndexOfAny(System.IO.Path.GetInvalidPathChars()) == -1) { //append ".log" extension to the end logFileName += ".log"; string fullPath = Path.GetFullPath(logFileName); string fileName = Path.GetFileName(fullPath); string pathName = Path.GetDirectoryName(fullPath); //check if the directory exists, if not, create it if (!Directory.Exists(pathName)) { Directory.CreateDirectory(pathName); } if (overwrite) { if (File.Exists(fullPath)) { //attempt to delete the file if it exists and overwrite is enabled try { File.Delete(fullPath); } catch (Exception e) { throw new Exception($"Unable to delete previous logfile because: {e.Message}"); } } } _logger.FileLogging = fileLog; _logger.LogFilename = fullPath; } else { throw new Exception("Logfile name or path contains invalid characters."); } } else { throw new Exception("Log filename is empty."); } } }