예제 #1
0
        /// <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 { }
            }
        }
예제 #2
0
        /// <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);
                }
            }
        }
예제 #3
0
        /// <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 { }
            }
        }
예제 #4
0
            /// <summary>
            /// Receives data from the client in async mode.
            /// </summary>
            private void AsyncReceived()
            {
                // If active.
                if (_context != null && _context.WebRequest != null && _context.WebRequest.Input != null)
                {
                    // If data exists.
                    if (_context.WebRequest.Input.Length > 0)
                    {
                        // Syslog message format '<priority>[version][_MessageLength] header message'.
                        // If using TCP then the _MessageLength is manditory, for UDP _MessageLength
                        // is optional.
                        byte[] buffer = new byte[_priorityNumber + _versionNumber + 1 + _receiveSize];

                        // Read the incomming log data.
                        int count = _context.WebRequest.Input.Read(buffer, 0, buffer.Length);

                        // For each byte read.
                        for (int i = 0; i < count; i++)
                        {
                            // If not start.
                            if (!_isStart)
                            {
                                // Find the start of the priority message.
                                if (buffer[i] == '<')
                                {
                                    _isStart = true;
                                    _priorityExtract[_priorityCount] = (byte)'<';
                                    _priorityCount++;

                                    // If the start is the last byte.
                                    if (i == (count - 1))
                                    {
                                        _isLastByte = true;
                                    }

                                    continue;
                                }
                            }

                            // If not end.
                            if (!_isEnd && _isStart)
                            {
                                // Find the end of the priority message.
                                if (buffer[i] == '>')
                                {
                                    _isEnd = true;
                                    _priorityExtract[_priorityCount] = (byte)'>';
                                    continue;
                                }
                            }

                            // If start of message has been found.
                            if (_isStart && !_priorityFound)
                            {
                                // If not end.
                                if (!_isEnd)
                                {
                                    _priorityExtract[_priorityCount] = buffer[i];
                                    _priorityCount++;
                                }

                                // If priority count is greater than number then
                                // not a priority.
                                if (_priorityCount > _priorityNumber - 1)
                                {
                                    _isStart       = false;
                                    _isEnd         = false;
                                    _priorityCount = 0;
                                    _priorityFound = false;
                                }
                            }

                            // If end of message has been found.
                            if (_isEnd && !_priorityFound)
                            {
                                // Get the priority.
                                _priorityFound = true;
                                string priorityValue = "";

                                // For each byte priority.
                                for (int j = 1; j < _priorityCount; j++)
                                {
                                    // Get the priority.
                                    priorityValue += Convert.ToChar(_priorityExtract[j]).ToString();
                                }

                                // Get the priority, facility and severity.
                                _priority = Convert.ToInt16(priorityValue.ToString());
                                _facility = SyslogUtility.GetFacility(_priority);
                                _severity = SyslogUtility.GetSeverity(_priority);
                            }

                            // If the message priority has been found.
                            if (_priorityFound)
                            {
                                // Keep count until the header
                                // used to find the version number.
                                _versionExtract[_versionCount] = buffer[i];
                                _versionCount++;

                                // Find the start of the header.
                                // A space after the version (if exists) number
                                // is the start of the header.
                                if (buffer[i] == ' ')
                                {
                                    // Found version if exists.
                                    _versionFound = true;
                                    _versionCount--;
                                }

                                // If found version, find out if it exits.
                                if (_versionFound)
                                {
                                    // Get the version if exists
                                    if (_versionCount > 0)
                                    {
                                        string versionValue = "";

                                        // For each byte priority.
                                        for (int j = 0; j < _versionCount; j++)
                                        {
                                            // Get the priority.
                                            versionValue += Convert.ToChar(_versionExtract[j]).ToString();
                                        }

                                        // Attempt to spit the version and message lenth.
                                        string[] split = versionValue.Split(new char[] { '_' }, StringSplitOptions.None);
                                        if (split.Length > 1)
                                        {
                                            // Contains a version and message length.
                                            // May contain a version number.
                                            if (!String.IsNullOrEmpty(split[0]))
                                            {
                                                // Get the version.
                                                _version = Convert.ToInt16(split[0]);
                                            }

                                            // Get the message length.
                                            _length = Convert.ToInt32(split[1]);
                                        }
                                        else
                                        {
                                            // Get the version.
                                            _version = Convert.ToInt16(split[0]);
                                            _length  = 0;
                                        }
                                    }

                                    // Reset the search.
                                    _isStart       = false;
                                    _isEnd         = false;
                                    _priorityCount = 0;
                                    _priorityFound = false;
                                    _versionCount  = 0;
                                    _versionFound  = false;

                                    // Indicates the begining of the message.
                                    _messageIndex    = 0;
                                    _messageStart    = true;
                                    _messageComplete = false;
                                }
                            }
                            else
                            {
                                // If the start of the message has been found.
                                if (_messageStart)
                                {
                                    // If the complete message has not been found.
                                    if (!_messageComplete)
                                    {
                                        // Add the message data to the store.
                                        _messageStore[_messageIndex] = buffer[i];
                                        _messageIndex++;

                                        // If a length exists then TCP wait for the message.
                                        if (_length > 0)
                                        {
                                            // The message is complete.
                                            if ((_messageIndex == _receiveSize) || (_messageIndex == _length))
                                            {
                                                // If a facility and severity has been found.
                                                if (_facility >= 0 && _severity >= 0)
                                                {
                                                    // Write the header and message for the current
                                                    // priority, facility and severity.
                                                    WriteLog(_facility, _severity,
                                                             (_length <= _receiveSize ? _messageStore.Take(_length).ToArray() : _messageStore.Take(_receiveSize).ToArray()));
                                                }

                                                // Reset the message handler.
                                                _messageIndex    = 0;
                                                _messageStart    = false;
                                                _messageComplete = true;
                                            }
                                        }
                                        else
                                        {
                                            // If no length exists then UDP not not wait,
                                            // write what has been received or get the
                                            // message until the start of a new message.
                                            // If start or then end of the data.
                                            if (_isStart || i == (count - 1))
                                            {
                                                // If a facility and severity has been found.
                                                if (_facility >= 0 && _severity >= 0)
                                                {
                                                    // Write the header and message for the current
                                                    // priority, facility and severity.
                                                    WriteLog(_facility, _severity, _messageStore.Take(_messageIndex).ToArray());
                                                }

                                                // Reset the message handler.
                                                _messageIndex    = 0;
                                                _messageStart    = false;
                                                _messageComplete = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        // If the last byte is '<' and length is not specified.
                        if (_isLastByte && _length == 0)
                        {
                            _isLastByte = false;

                            // If a facility and severity has been found.
                            if (_facility >= 0 && _severity >= 0)
                            {
                                // Write the header and message for the current
                                // priority, facility and severity.
                                WriteLog(_facility, _severity, _messageStore.Take(_messageIndex).ToArray());
                            }

                            // Reset the message handler.
                            _messageIndex    = 0;
                            _messageStart    = false;
                            _messageComplete = true;
                        }
                    }
                }
            }