public EventLogAppender(string name, AppenderInitializationData configInitialisationData) : base(name) { if (configInitialisationData == null) { LogManager.LogInternalEvent(EventLevel.Error, "The appender requires .... It will be disabled", null); this.IsValid = false; return; } source = configInitialisationData.GetInitialisationElementValue<string>("Source"); if (source == null) { LogManager.LogInternalEvent(EventLevel.Error, "The appender requires a Source parameter which is not present. It will be disabled", null); this.IsValid = false; return; } log = configInitialisationData.GetInitialisationElementValue<string>("Log"); if (log == null) { LogManager.LogInternalEvent(EventLevel.Error, "The appender requires a Log parameter which is not present. It will be disabled", null); this.IsValid = false; return; } if (!EventLog.SourceExists(source)) { EventLog.CreateEventSource(source, log); LogManager.LogInternalEvent(EventLevel.Info, string.Format("Event source {0} / log {1} does not exist - creating now", source, log), null); } }
public void AppenderInitializationDataReturnsValidTypedValue() { int expectedNumber = 1; string configData = @"<Params><Name>Fred</Name><Number>1</Number></Params>"; AppenderInitializationData apData = new AppenderInitializationData(configData); Assert.AreEqual(expectedNumber, apData.GetInitialisationElementValue<Int32>("Number")); }
public void AppenderInitializationDataReturnsValidConfigNode() { string expectedNodeName = "Fred"; string configData = @"<Params><Name>Fred</Name><Number>1</Number></Params>"; AppenderInitializationData apData = new AppenderInitializationData(configData); Assert.AreEqual(expectedNodeName, apData.GetInitialisationElementAsNode("Name").InnerText); }
public void AppenderInitializationDataReturnsDefaultValueForInvalidCast() { int expectedNumber = default(int); string configData = @"<Params><Name>Fred</Name><Number>1</Number></Params>"; AppenderInitializationData apData = new AppenderInitializationData(configData); Assert.AreEqual(expectedNumber, apData.GetInitialisationElementValue<Int32>("Name")); }
public SqlDbAppender(string name, AppenderInitializationData configInitialisationData) : base(name) { if (configInitialisationData == null) { LogManager.LogInternalEvent(EventLevel.Error, "The appender requires initialization data which was not supplied (invalid config?). It will be disabled", null); this.IsValid = false; return; } connectionString = configInitialisationData.GetInitialisationElementValue<string>("dbConnectionString"); if (string.IsNullOrEmpty(connectionString)) { LogManager.LogInternalEvent(EventLevel.Error, "No connection string was found in the initialisation data. Appender will be disabled.", null); this.IsValid = false; return; } // Check Db // Set default insert statement insertStatement = "insert LogEvent (DateLogged, Severity, Location, Message, Data) values (@dateLogged, @severity, @location, @message, @data)"; }
public ConsoleAppender(string name, AppenderInitializationData configInitialisationData) : base(name) { MsgOnly = configInitialisationData.GetInitialisationElementValue<bool>("MsgOnlyFormat"); }
public InvalidAppender(string name, AppenderInitializationData apInitData) : this(name) { }
public SmtpAppender(string name, AppenderInitializationData configInitialisationData) : base(name) { if (configInitialisationData == null) { LogManager.LogInternalEvent(EventLevel.Error, "The appender requires .... It will be disabled", null); this.IsValid = false; return; } toAddress = configInitialisationData.GetInitialisationElementValue<string>("ToAddress"); if (toAddress == null) { LogManager.LogInternalEvent(EventLevel.Error, "The appender requires a ToAddress parameter which is not present. It will be disabled", null); this.IsValid = false; return; } fromAddress = configInitialisationData.GetInitialisationElementValue<string>("FromAddress"); if (fromAddress == null) { LogManager.LogInternalEvent(EventLevel.Error, "The appender requires a FromAddress parameter which is not present. It will be disabled", null); this.IsValid = false; return; } subject = configInitialisationData.GetInitialisationElementValue<string>("Subject"); if (subject == null) { LogManager.LogInternalEvent(EventLevel.Error, "The appender requires a Subject parameter which is not present. It will be disabled", null); this.IsValid = false; return; } smtpServer = configInitialisationData.GetInitialisationElementValue<string>("SMTPServer"); if (smtpServer == null) { LogManager.LogInternalEvent(EventLevel.Error, "The appender requires a SMTPServer parameter which is not present. It will be disabled", null); this.IsValid = false; return; } bool? isHtmlFromConfig = configInitialisationData.GetInitialisationElementValue<bool>("IsHtml"); if (isHtmlFromConfig == null) { isHtml = isHtmlFromConfig.Value; } string timeoutFromConfig = configInitialisationData.GetInitialisationElementValue<string>("Timeout"); if (!string.IsNullOrEmpty(timeoutFromConfig)) { bool ok = int.TryParse(timeoutFromConfig, out timeout); if (!ok) { LogManager.LogInternalEvent(EventLevel.Error, string.Format("Timeout was supplied but invalid, using default ({0})", timeout), null); } } client = new SmtpClient(smtpServer); client.Timeout = timeout; client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); //client.DeliveryMethod = SmtpDeliveryMethod. //client.EnableSsl = //client.Port = //client.Timeout //client.UseDefaultCredentials = }
/// <summary> /// Create a new instance of a FileLogger component. /// </summary> /// <param name="name"></param> /// <param name="setLogEventFormatter"></param> /// <param name="configParams"></param> public FileAppender(string name, AppenderInitializationData configInitialisationData) : base(name) { if (configInitialisationData == null) { LogManager.LogInternalEvent(EventLevel.Error, "The appender requires configuration data that was not provided. It will be disabled", null); this.IsValid = false; return; } // Get the file path string tmpFilePath = configInitialisationData.GetInitialisationElementValue<string>("FilePath"); if (string.IsNullOrEmpty(tmpFilePath)) { throw new ConfigurationException("The file appender path was not provided."); } // If there is a {Timestamp} in the path, replace with formatted datetime if (tmpFilePath.IndexOf(TIME_STAMP_PLACEHOLDER) >= 0) { tmpFilePath = tmpFilePath.Replace(TIME_STAMP_PLACEHOLDER, DateTime.Now.ToString(DATETIME_FORMAT)); } // Store the original file path originalFilePath = Path.GetFileName(tmpFilePath); // If relative, attempt to convert to absolute path tmpFilePath = GetAbsoluteFilePath(tmpFilePath); // And store the directory portion directoryPath = Path.GetDirectoryName(tmpFilePath); // Are we going to overwrite any existing files? overwriteExistingFile = configInitialisationData.GetInitialisationElementValue<bool>("OverwriteExistingFile"); // If overwriteExistingFile is disabled, create a new unique file name if (!overwriteExistingFile) GetUniqueFilePath(ref tmpFilePath); // Create the directory if it doesn't exist if (!Directory.Exists(Path.GetDirectoryName(tmpFilePath))) Directory.CreateDirectory(Path.GetDirectoryName(tmpFilePath)); filePath = tmpFilePath; // Get the max file size. if not supplied, default is 0L. MaxFileSizeBytes = configInitialisationData.GetInitialisationElementValue<long>("MaxFileSizeBytes"); LogManager.LogInternalEvent(EventLevel.Info, string.Format("Config specifies {0} as max file size", MaxFileSizeBytes), null); if (MaxFileSizeBytes == 0) MaxFileSizeBytes = GetMaxFileSize(filePath); if (this.IsValid) { LogManager.LogInternalEvent(EventLevel.Info, string.Format("FileAppender {0} successfully created at {1}. Overwrite is {2}, MaxFileSize={3}", this.Name, this.filePath, this.overwriteExistingFile, // this.lockMode, this.maxFileSize == NO_MAX_FILE_SIZE ? "None" : this.maxFileSize.ToString()), null); } }