Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Collect and Compare Log Statistics using LogJoin

, 22 Oct 2013
The LogJoin tool helps to collect any unstructured data from text files and join it to a simple table representation for easy analysis.
/*
 * Copyright (c) 2013, Yuriy Nelipovich
 * 
 * If you find this code useful or in case of any questions, suggestions
 * bug reports, donation, please email me: dev.yuriy.n@gmail.com
 */

using System;
using System.Configuration;
using System.IO;
using System.Linq;
using LogJoin.Config;

namespace LogJoin
{
    internal class Program
    {
        /// <summary>
        /// Starting point
        /// </summary>
        /// <param name="args">The first argument defines DateTime value for input and output file names patterns</param>
        private static void Main(string[] args)
        {
            DateTime date;
            if (args.Length == 0 || args[0] == null || !DateTime.TryParse(args[0], out date))
            {
                date = DateTime.Now;
            }

            var parameters = (Parameters) ConfigurationManager.GetSection("parameters");

            var outputFilePathWithDate = string.Format(parameters.OutputFilePath, date);

            var inputs = parameters.Inputs.Select(
                inputAttrs =>
                    {
                        var fileNamePattern = string.Format(inputAttrs.FileNameMask, date);
                        return
                            new
                                {
                                    InputAttrs = inputAttrs,
                                    Input = HandleOperation(
                                        () => new MultilineInput(
                                                  Directory.EnumerateFiles(inputAttrs.DirPath,
                                                                           fileNamePattern,
                                                                           SearchOption.AllDirectories)
                                                           .OrderBy(f => f).ToArray(),
                                                  inputAttrs.MaxLinesCount),
                                        string.Format("Path: {0}; File: {1}", inputAttrs.DirPath, fileNamePattern))
                                };
                    }).ToArray();

            var sources = inputs.Select(
                input =>
                    {
                        var regEx = parameters.BuildRegEx(input.InputAttrs.RegExName);
                        return new Source(input.Input, input.InputAttrs.SourceName,
                                          regEx.Value, regEx.IsMultiline,
                                          input.InputAttrs.KeyNames, input.InputAttrs.ColumnNames);
                    });

            using (new AggregatedDisposable(inputs.Select(i => i.Input).ToArray()))
            using (var output =
                HandleOperation(
                    () => new FileStream(outputFilePathWithDate, FileMode.CreateNew, FileAccess.Write),
                    string.Format("File: {0}", outputFilePathWithDate)))
            {
                var exporter = new Join(sources.ToArray(), parameters.OutputValuesDelimiter);
                exporter.Export(output);
            }

            Console.WriteLine("Done");
        }

        /// <summary>
        /// Wraps some excepions with context-specific information
        /// </summary>
        /// <typeparam name="T">Type of result</typeparam>
        /// <param name="operation">Operation with expected error</param>
        /// <param name="errorDetails">Operation context information</param>
        /// <returns></returns>
        private static T HandleOperation<T>(Func<T> operation, string errorDetails)
        {
            try
            {
                return operation();
            }
            catch (IOException e)
            {
                throw new IOException(errorDetails, e);
            }
            catch (ArgumentException e)
            {
                throw new ArgumentException(errorDetails, e);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

Yuriy Nelipovich
Software Developer CactusSoft
Belarus Belarus
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 22 Oct 2013
Article Copyright 2013 by Yuriy Nelipovich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid