Exemplo n.º 1
0
        /// <summary>
        /// Initializes the log for function. Creates the log file with a first entry.
        /// </summary>
        /// <param name="cfg"></param>
        public static void Initialize(LogConfiguration cfg = null)
        {
            if (cfg == null)
            {
                cfg = new LogConfiguration();
            }

            NewInstance(cfg);

            try
            {
                using (StreamWriter file = new StreamWriter(FullLogPath, true))
                {
                    file.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}{Delimiter}{TypeInfo}{Delimiter}{AppLogName} Initialized");
                }
            }
            catch (Exception e)
            {
                throw new Exception($"Log Exception [Log].[Initialize()]: {e.ToString()}");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Set interal values and creates the full log file path used for the first write.
        /// </summary>
        /// <param name="cfg"></param>
        private static void NewInstance(LogConfiguration cfg)
        {
            try
            {
                LogFileName = string.Empty;
                FullLogPath = string.Empty;
                Delimiter   = string.Empty;
                AppLogName  = string.Empty;

                Delimiter      = cfg.Delimiter;
                AppLogName     = cfg.LogName;
                WriteToConsole = cfg.WriteToConsole;
                WriteToLog     = cfg.WriteToLog;

                LogFileName = $"{AppLogName}-{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.txt";

                if (cfg.Directory == "default")
                {
                    FullLogPath = Directory.GetCurrentDirectory() + @"\" + LogFileName;
                }
                else
                {
                    FullLogPath = cfg.Directory + @"\" + LogFileName;

                    if (!Directory.Exists(cfg.Directory))
                    {
                        throw new Exception($"Log Exception [Log].[NewInstance()]: Directory doesn't exist! Directory = {cfg.Directory}");
                    }
                }

                Initialized = true;
            }
            catch (Exception e)
            {
                throw new Exception($"Log Exception [Log].[SetLogFile()]: {e.ToString()}");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static LogConfiguration GenerateConfig()
        {
            var cfg = new LogConfiguration();

            return(cfg);
        }