Exemplo n.º 1
0
        public static int Generate(JsonLogFileTestParameters parms)
        {
            LogManager.GetCurrentClassLogger().Info("Start LogFile Generation for: {0} on Thread: {1}", Path.GetFullPath(parms.LogFileName), Thread.CurrentThread.ManagedThreadId);

            var logFilePath = Path.Combine(parms.LogFileDir, parms.LogFileName);

            try
            {
                if (File.Exists(logFilePath))
                {
                    LogManager.GetCurrentClassLogger().Info("Deleting file: {0}", logFilePath);
                    File.Delete(logFilePath);
                }
            }
            catch (Exception ex)
            {
                LogManager.GetCurrentClassLogger().Error(ex);
            }

            var hostName = System.Environment.MachineName + "." +
                   Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
                       "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters").GetValue("Domain", "").ToString();

            var watch = Stopwatch.StartNew();

            // This text is always added, making the file longer over time
            // if it is not deleted.
            using (StreamWriter sw = File.AppendText(logFilePath))
            {
                for (int i = 0; i < parms.NumMessages; i++)
                {
                    JObject o = new JObject
                    {
                        {"LineNumber", i+1},
                        {"Application", "logfile-generator"},
                        {"Host", hostName},
                        {"UtcTimestamp", DateTime.UtcNow.ToString("o")},
                        {"Type", "log"},
                        {"Message", string.Format("{0}: Testgenerator logfile message {1}", i+1, DateTime.UtcNow.ToString("o"))},
                        {"Index", "logstash"}
                    };
                    sw.WriteLine(o.ToString(Formatting.None));

                    Thread.Sleep(parms.SleepTimeMilliseconds);
                }
                LogManager.GetCurrentClassLogger().Info("Elapsed Time for {0} was {1} seconds", Path.GetFullPath(parms.LogFileName), watch.Elapsed);
                watch.Reset();
            }

            LogManager.GetCurrentClassLogger().Info("Finished LogFile Generation for: {0} elapsed: {1}", Path.GetFullPath(parms.LogFileName), watch.Elapsed);

            return parms.NumMessages;
        }
Exemplo n.º 2
0
        private static void StartJsonRolling(CommandLineOptions options)
        {
            if (options.JsonRollingLogFiles.Length > 0)
            {
                foreach (var logFile in options.JsonRollingLogFiles)
                {
                    _totalMessagesToSend += options.NumMessages;

                    if (options.Verbose)
                        LogManager.GetCurrentClassLogger()
                            .Info("Starting RollingLogFile Generator for {0}",
                                Path.GetFullPath(Path.Combine(options.JsonLogDir, logFile)));
                    _tasks.Add(Task.Factory.StartNew(() =>
                    {
                        var p = new JsonLogFileTestParameters()
                        {
                            NumMessages = options.NumMessages,
                            LogFileDir = options.JsonLogDir,
                            LogFileName = logFile,
                            SleepTimeMilliseconds = options.JsonRate
                        };
                        JsonRollingLogFileGenerator.Generate(p);
                        Thread.Sleep(250);
                    }));

                }
            }
        }
        public static int Generate(JsonLogFileTestParameters parms)
        {
            LogManager.GetCurrentClassLogger().Info("Start JSON RollingLogFile Generation for: {0} on Thread: {1}", Path.GetFullPath(parms.LogFileName), Thread.CurrentThread.ManagedThreadId);

            var logFilePath = Path.Combine(parms.LogFileDir, parms.LogFileName);

            try
            {
                if (File.Exists(logFilePath))
                    File.Delete(logFilePath);

                if (File.Exists(logFilePath + ".rolled"))
                    File.Delete(logFilePath + ".rolled");
            }
            catch (Exception ex)
            {
                LogManager.GetCurrentClassLogger().Error(ex);
            }

            var hostName = System.Environment.MachineName + "." +
                   Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
                       "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters").GetValue("Domain", "").ToString();

            int quarters = parms.NumMessages/4;

            int[] segments = new int[] {quarters, quarters, quarters, quarters + parms.NumMessages%4};
            var watch = Stopwatch.StartNew();

            int recordNumber = 0;
            int currentTotal = 0;
            for (int segment = 0; segment < 4; segment++)
            {
                currentTotal += segments[segment];

                // This text is always added, making the file longer over time
                // if it is not deleted.
                using (StreamWriter sw = File.AppendText(logFilePath))
                {
                    sw.AutoFlush = true;

                    var lwatch = Stopwatch.StartNew();

                    // The Rolling Generator will roll 1/2 way through the log
                    for (int i = 0; i < segments[segment]; i++)
                    {
                        JObject o = new JObject
                        {
                            {"LineNumber", recordNumber + 1},
                            {"Application", "jsonrollinglogfile-generator"},
                            {"Host", hostName},
                            {"UtcTimestamp", DateTime.UtcNow.ToString("o")},
                            {"Type", "jsonrollinglog"},
                            {
                                "Message",
                                string.Format("{0}: Testgenerator jsonrollinglogfile message {1}", recordNumber + 1,
                                    DateTime.UtcNow.ToString("o"))
                            },
                            {"Index", "logstash"}
                        };
                        sw.WriteLine(o.ToString(Formatting.None));
                        recordNumber++;
                        Thread.Sleep(parms.SleepTimeMilliseconds);
                    }
                    LogManager.GetCurrentClassLogger().Info("Elapsed Time for {0} was {1} seconds for {2} logs", Path.GetFullPath(parms.LogFileName), lwatch.Elapsed, segments[segment]);

                }

                //
                // We might not have yet processed all the lines from the first file, so wait till
                // we catch up before rolling the log file.
                //
                LogManager.GetCurrentClassLogger().Info("{0}: Waiting for output to catch up: {1} {2}", Thread.CurrentThread.ManagedThreadId, logFilePath, currentTotal);
                WaitOutputToCatchUp(logFilePath, currentTotal);

                //
                // Roll the log + wait for the reader to catch up.
                //

                LogManager.GetCurrentClassLogger().Info("{0}: Rolling Log File: {1} {2}", Thread.CurrentThread.ManagedThreadId, logFilePath, File.GetCreationTimeUtc(logFilePath));

                RollLogFile(logFilePath);

                LogManager.GetCurrentClassLogger().Info("{0}: Finished Rolling Log File: {1}", Thread.CurrentThread.ManagedThreadId, logFilePath);
            }

            watch.Stop();

            LogManager.GetCurrentClassLogger().Info("Finished JSON RollingLogFile File Generation for: {0} elapsed: {1}", Path.GetFullPath(parms.LogFileName), watch.Elapsed);

            return parms.NumMessages;
        }