/// <summary> /// Write the data to the log entity. /// </summary> /// <param name="priority">The current log priority.</param> /// <param name="data">The data to write.</param> private void Write(short priority, byte[] data) { try { // Try and get the file. Stream stream = null; bool ret = _syslogFiles.TryGetValue(priority, out stream); if (ret) { // Write the data. stream.Write(data, 0, data.Length); } else { // File can not be found. throw new Exception(); } } catch { try { // Attempt to erite to the internal error log. short prioritySystem = SyslogUtility.GetPriority(5, 2); // Try and get the file. Stream fileSystem = null; bool retSystem = _syslogFiles.TryGetValue(prioritySystem, out fileSystem); if (retSystem) { // Write to this log. DateTime timeSystem = DateTime.Now; byte[] buffer = Encoding.Default.GetBytes( timeSystem.ToShortDateString() + " " + timeSystem.ToShortTimeString() + " " + Environment.MachineName + " " + "Could not open priority log file : " + priority.ToString() + "\r\n"); // Write the data to the file. fileSystem.Write(buffer, 0, buffer.Length); } } catch { } } }
/// <summary> /// Write the data to the log entity. /// </summary> /// <param name="facility">The syslog facility.</param> /// <param name="severity">The syslog severity.</param> /// <param name="data">The data to write.</param> private void WriteLog(short facility, short severity, byte[] data) { // Make sure data exists. if (data != null && data.Length > 0) { // Only allow one thread at a time // to write data to a log file. lock (_writeLogLock) { // Get the current priority. short priority = SyslogUtility.GetPriority(facility, severity); // Determine if the priority file should be archived. ArchiveFile(priority); // Write the data. Write(priority, data); } } }
/// <summary> /// Archive the priority file. /// </summary> /// <param name="priority">The current log priority.</param> private void ArchiveFile(short priority) { try { // Try and get the file. Stream stream = null; bool ret = _syslogFiles.TryGetValue(priority, out stream); if (ret) { // Get the file name. string directory = _systemLogPath.Path.TrimEnd(new char[] { '\\' }); string file = directory + "\\" + priority.ToString() + ".log"; // If the maximum file size has been reached. FileInfo fileInfo = new FileInfo(file); if (fileInfo.Length >= _systemLogPath.MaximumFileSize) { // Archive the file. try { // Release the file. stream.Close(); stream.Dispose(); } catch { } try { // Get the archive file. DateTime dateTimeArchive = DateTime.Now; string directoryArchive = _systemLogPath.ArchivePath.TrimEnd(new char[] { '\\' }) + "\\" + dateTimeArchive.Year.ToString() + "\\" + dateTimeArchive.Month.ToString() + "\\" + dateTimeArchive.Day.ToString(); // Get the file archive. string fileArchive = directoryArchive + "\\" + priority.ToString() + "_" + dateTimeArchive.Hour.ToString() + "-" + dateTimeArchive.Minute.ToString() + "-" + dateTimeArchive.Second.ToString() + ".log"; // If directory does not exists. if (!Directory.Exists(directoryArchive)) { // Create the directory. Directory.CreateDirectory(directoryArchive); } // Move the file. File.Move(file, fileArchive); } catch { } try { // Create a new priority log file. // Add the file to the files list. _syslogFiles.TryUpdate(priority, new FileStream(file, FileMode.Append, FileAccess.Write), stream); } catch { } } } else { // File can not be found. throw new Exception(); } } catch { try { // Attempt to erite to the internal error log. short prioritySystem = SyslogUtility.GetPriority(5, 2); // Try and get the file. Stream fileSystem = null; bool retSystem = _syslogFiles.TryGetValue(prioritySystem, out fileSystem); if (retSystem) { // Write to this log. DateTime timeSystem = DateTime.Now; byte[] buffer = Encoding.Default.GetBytes( timeSystem.ToShortDateString() + " " + timeSystem.ToShortTimeString() + " " + Environment.MachineName + " " + "Could not open priority log file : " + priority.ToString() + "\r\n"); // Write the data to the file. fileSystem.Write(buffer, 0, buffer.Length); } } catch { } } }