Exemplo n.º 1
0
        // NEOLOGGER CLASS
        public NeoLogger (string[] args) {

            // Parse the Command Line Arguments
            this.parseCommandLine(args);
        
            // Creating the syslogsender object
            this.neolog = new SyslogClient.Client();

            // Setting Parameters
            //neolog.RemoteAddress = ip;
            //neolog.RemotePort = port;
            //neolog.PriorityNumber = priority;
            //neolog.FacilityName = facility;
            //neolog.UseUTF8 = true;
            neolog.SysLogServerIp = ip;
            neolog.Port = port;

            // File Positions Dictionary
            filePositions = new Dictionary<string, long>();

// INPUT ######################################################################

            // Input Stream
            if (readType == 1)
            {
                string line;
                // Read input stream lines
                while ((line = Console.ReadLine()) != null)
                {
                    verifiedSend(line);
                }
            }

            // Single Message
            if (readType == 2)
            {
                send(message);
            }

            // File Input
            if (readType == 3)
            {
                // Read only tail lines ----------------------------------------------------
                if (onlyTail)
                {

                    // Open the file
                    StreamReader filestream = null;
                    filestream = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
                    // Got to the End of the file
                    filestream.BaseStream.Seek(0, SeekOrigin.End);
                    // Save position
                    filePositions.Add(file, filestream.BaseStream.Position);
                    filestream.Close();

                    // Create a watcher
                    FileSystemWatcher watcher = new FileSystemWatcher();
                    watcher.Path = Path.GetDirectoryName(file);
                    watcher.Filter = Path.GetFileName(file);
                    watcher.NotifyFilter = NotifyFilters.LastWrite;

                    // Add event handlers.	 
                    watcher.Changed += new FileSystemEventHandler(OnChangedFile);
                    // Start watching
                    watcher.EnableRaisingEvents = true;
                    // watcher.WaitForChanged(WatcherChangeTypes.Changed);

                    // Print hint and wait
                    printHint("Stop sending by pressing 'q'");

                }
                // Read only new lines --------------------------------------------------
                else if (onlyNew)
                {

                    // Generate file name
                    string statFile = Path.GetFileName(file) + ".stat";

                    // If stat file exists and contains a last position
                    long savedPos = getOldFilePosFromStat(statFile);
                    // Check if file has been deleted or shrinked in the meantime
                    bool isNewFile = checkFileNew(savedPos);

                    long lastPos = 0;
                    if (  savedPos > 0 && ! isNewFile ) {
                        lastPos = sendFile(this.file, savedPos);
                    }
                    else
                    {
                        lastPos = sendFile(this.file, 0);
                    }

                    // Save the last file pointer position
                    setFilePosToStat(lastPos, statFile);

                }

                // Just read the file and then exit
                else
                {
                    // Send the whole content
                    sendFile(this.file, 0);

                }
            }

            // Eventlog -------------------------------------------------------
            if (readType == 4)
            {
                try {
                    
                    // Open Eventlog
                    ev = new EventLog(eventlogType, System.Environment.MachineName);
                    int LastLogToShow = ev.Entries.Count;
                    if (LastLogToShow <= 0 && debug && !onlyTail)
                        Console.WriteLine("No Event Logs in the Log :" + eventlogType);

                    if (onlyTail)
                    {
                        // Set pointer pos to current size
                        eventlogPos = ev.Entries.Count;

                        ev.EntryWritten += new EntryWrittenEventHandler(OnEventlogChange);
                        ev.EnableRaisingEvents = true;

                        printHint("Stop sending by pressing 'q'");
                    }
                    else if (onlyNew)
                    {
                        // Generate file name
                        string statFile = eventlogType + ".stat";

                        // If file exists
                        if (File.Exists(statFile))
                        {

                            int oldIndex = getOldEventlogPosFromStat(statFile);
                            print("Stat file found. Event Record Id " + oldIndex + " read");
                            // If Eventlog is empty
                            if (ev.Entries.Count < 1)
                            {
                                print("Wanring: Eventlog is empty! Deleting stat file.");
                                File.Delete(statFile);
                            } else {
                                if (oldIndex < ev.Entries[ev.Entries.Count - 1].Index)
                                {
                                    long lastIndex = sendEventlogFromIndex(oldIndex);
                                    File.WriteAllText(statFile, lastIndex.ToString());
                                    if (lastIndex == 0)
                                    {
                                        send("NeoLogger Wanring - Windows Eventlog rotates to fast! Try increasing the eventlog size");
                                    }
                                }
                            }
                        }
                        else
                        {
                            print("No stat file for this Eventlog found. File will be created. Next time NeoLogger is invoked, only new entries will be sent.");
                            File.WriteAllText(statFile, ev.Entries[ev.Entries.Count-1].Index.ToString());
                        }
                    }
                    else
                    {
                        sendEventlog(0);
                    }
                    ev.Close();
                }
                catch (Exception ex)
                {
                    showErrorAndExit("Eventlog with name " + eventlogType + " not found or not accessible. " + ex.ToString());
                }


            }

            // Directory Watcher ----------------------------------------------
            if (readType == 5)
            {
                // Create a watcher
                FileSystemWatcher watcher = new FileSystemWatcher();
                watcher.Path = dir;
                watcher.Filter = file_filter;
                if (includeSubdirectories)
                {
                    watcher.IncludeSubdirectories = true;
                }
                watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

                // Add event handlers.	 
                watcher.Changed += new FileSystemEventHandler(OnWatcherChangedFile);
                watcher.Created += new FileSystemEventHandler(OnWatcherChangedFile);
                watcher.Deleted += new FileSystemEventHandler(OnWatcherChangedFile);
                watcher.Renamed += new RenamedEventHandler(OnWatcherRenamed);

                // Start watching
                watcher.EnableRaisingEvents = true;
                // watcher.WaitForChanged(WatcherChangeTypes.Changed);

                // Print hint and wait
                printHint("Stop sending by pressing 'q'");

            }

            // Read Directory -------------------------------------------------
            if (readType == 6)
            {
                if (onlyTail)
                {
                    // Walk the directory and save the last positions of the the filesto the dictionary
                    DirectoryInfo directory = new DirectoryInfo(dir);
                    WalkDirectoryTree(directory);

                    // Create a watcher
                    FileSystemWatcher watcher = new FileSystemWatcher();
                    watcher.Path = dir;
                    watcher.Filter = file_filter;
                    if (includeSubdirectories)
                    {
                        watcher.IncludeSubdirectories = true;
                    }
                    watcher.NotifyFilter = NotifyFilters.LastWrite;

                    // Add event handlers.	 
                    watcher.Changed += new FileSystemEventHandler(OnChangedFile);
                    watcher.Created += new FileSystemEventHandler(OnChangedFile);

                    // Start watching
                    watcher.EnableRaisingEvents = true;
                    // watcher.WaitForChanged(WatcherChangeTypes.Changed);

                    // Print hint and wait
                    printHint("Stop sending by pressing 'q'");
                }
                // Send all files content
                else
                {
                    // Walk the directory and send the file contents
                    DirectoryInfo directory = new DirectoryInfo(dir);
                    WalkDirectoryTree(directory);
                }

            }

            // Close Socket
            neolog.Close();

        }
Exemplo n.º 2
0
        // NEOLOGGER CLASS
        public NeoLogger(string[] args)
        {
            // Parse the Command Line Arguments
            this.parseCommandLine(args);

            // Creating the syslogsender object
            this.neolog = new SyslogClient.Client();

            // Setting Parameters
            //neolog.RemoteAddress = ip;
            //neolog.RemotePort = port;
            //neolog.PriorityNumber = priority;
            //neolog.FacilityName = facility;
            //neolog.UseUTF8 = true;
            neolog.SysLogServerIp = ip;
            neolog.Port           = port;

            // File Positions Dictionary
            filePositions = new Dictionary <string, long>();

// INPUT ######################################################################

            // Input Stream
            if (readType == 1)
            {
                string line;
                // Read input stream lines
                while ((line = Console.ReadLine()) != null)
                {
                    verifiedSend(line);
                }
            }

            // Single Message
            if (readType == 2)
            {
                send(message);
            }

            // File Input
            if (readType == 3)
            {
                // Read only tail lines ----------------------------------------------------
                if (onlyTail)
                {
                    // Open the file
                    StreamReader filestream = null;
                    filestream = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
                    // Got to the End of the file
                    filestream.BaseStream.Seek(0, SeekOrigin.End);
                    // Save position
                    filePositions.Add(file, filestream.BaseStream.Position);
                    filestream.Close();

                    // Create a watcher
                    FileSystemWatcher watcher = new FileSystemWatcher();
                    watcher.Path         = Path.GetDirectoryName(file);
                    watcher.Filter       = Path.GetFileName(file);
                    watcher.NotifyFilter = NotifyFilters.LastWrite;

                    // Add event handlers.
                    watcher.Changed += new FileSystemEventHandler(OnChangedFile);
                    // Start watching
                    watcher.EnableRaisingEvents = true;
                    // watcher.WaitForChanged(WatcherChangeTypes.Changed);

                    // Print hint and wait
                    printHint("Stop sending by pressing 'q'");
                }
                // Read only new lines --------------------------------------------------
                else if (onlyNew)
                {
                    // Generate file name
                    string statFile = Path.GetFileName(file) + ".stat";

                    // If stat file exists and contains a last position
                    long savedPos = getOldFilePosFromStat(statFile);
                    // Check if file has been deleted or shrinked in the meantime
                    bool isNewFile = checkFileNew(savedPos);

                    long lastPos = 0;
                    if (savedPos > 0 && !isNewFile)
                    {
                        lastPos = sendFile(this.file, savedPos);
                    }
                    else
                    {
                        lastPos = sendFile(this.file, 0);
                    }

                    // Save the last file pointer position
                    setFilePosToStat(lastPos, statFile);
                }

                // Just read the file and then exit
                else
                {
                    // Send the whole content
                    sendFile(this.file, 0);
                }
            }

            // Eventlog -------------------------------------------------------
            if (readType == 4)
            {
                try {
                    // Open Eventlog
                    ev = new EventLog(eventlogType, System.Environment.MachineName);
                    int LastLogToShow = ev.Entries.Count;
                    if (LastLogToShow <= 0 && debug && !onlyTail)
                    {
                        Console.WriteLine("No Event Logs in the Log :" + eventlogType);
                    }

                    if (onlyTail)
                    {
                        // Set pointer pos to current size
                        eventlogPos = ev.Entries.Count;

                        ev.EntryWritten       += new EntryWrittenEventHandler(OnEventlogChange);
                        ev.EnableRaisingEvents = true;

                        printHint("Stop sending by pressing 'q'");
                    }
                    else if (onlyNew)
                    {
                        // Generate file name
                        string statFile = eventlogType + ".stat";

                        // If file exists
                        if (File.Exists(statFile))
                        {
                            int oldIndex = getOldEventlogPosFromStat(statFile);
                            print("Stat file found. Event Record Id " + oldIndex + " read");
                            // If Eventlog is empty
                            if (ev.Entries.Count < 1)
                            {
                                print("Wanring: Eventlog is empty! Deleting stat file.");
                                File.Delete(statFile);
                            }
                            else
                            {
                                if (oldIndex < ev.Entries[ev.Entries.Count - 1].Index)
                                {
                                    long lastIndex = sendEventlogFromIndex(oldIndex);
                                    File.WriteAllText(statFile, lastIndex.ToString());
                                    if (lastIndex == 0)
                                    {
                                        send("NeoLogger Wanring - Windows Eventlog rotates to fast! Try increasing the eventlog size");
                                    }
                                }
                            }
                        }
                        else
                        {
                            print("No stat file for this Eventlog found. File will be created. Next time NeoLogger is invoked, only new entries will be sent.");
                            File.WriteAllText(statFile, ev.Entries[ev.Entries.Count - 1].Index.ToString());
                        }
                    }
                    else
                    {
                        sendEventlog(0);
                    }
                    ev.Close();
                }
                catch (Exception ex)
                {
                    showErrorAndExit("Eventlog with name " + eventlogType + " not found or not accessible. " + ex.ToString());
                }
            }

            // Directory Watcher ----------------------------------------------
            if (readType == 5)
            {
                // Create a watcher
                FileSystemWatcher watcher = new FileSystemWatcher();
                watcher.Path   = dir;
                watcher.Filter = file_filter;
                if (includeSubdirectories)
                {
                    watcher.IncludeSubdirectories = true;
                }
                watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

                // Add event handlers.
                watcher.Changed += new FileSystemEventHandler(OnWatcherChangedFile);
                watcher.Created += new FileSystemEventHandler(OnWatcherChangedFile);
                watcher.Deleted += new FileSystemEventHandler(OnWatcherChangedFile);
                watcher.Renamed += new RenamedEventHandler(OnWatcherRenamed);

                // Start watching
                watcher.EnableRaisingEvents = true;
                // watcher.WaitForChanged(WatcherChangeTypes.Changed);

                // Print hint and wait
                printHint("Stop sending by pressing 'q'");
            }

            // Read Directory -------------------------------------------------
            if (readType == 6)
            {
                if (onlyTail)
                {
                    // Walk the directory and save the last positions of the the filesto the dictionary
                    DirectoryInfo directory = new DirectoryInfo(dir);
                    WalkDirectoryTree(directory);

                    // Create a watcher
                    FileSystemWatcher watcher = new FileSystemWatcher();
                    watcher.Path   = dir;
                    watcher.Filter = file_filter;
                    if (includeSubdirectories)
                    {
                        watcher.IncludeSubdirectories = true;
                    }
                    watcher.NotifyFilter = NotifyFilters.LastWrite;

                    // Add event handlers.
                    watcher.Changed += new FileSystemEventHandler(OnChangedFile);
                    watcher.Created += new FileSystemEventHandler(OnChangedFile);

                    // Start watching
                    watcher.EnableRaisingEvents = true;
                    // watcher.WaitForChanged(WatcherChangeTypes.Changed);

                    // Print hint and wait
                    printHint("Stop sending by pressing 'q'");
                }
                // Send all files content
                else
                {
                    // Walk the directory and send the file contents
                    DirectoryInfo directory = new DirectoryInfo(dir);
                    WalkDirectoryTree(directory);
                }
            }

            // Close Socket
            neolog.Close();
        }