private void InitializeLogFile()
        {
            _semaphoreSlim.Wait();

            try
            {
                LogFileName = Path.Combine(_workingFolder, $"WTS_{Configuration.Current.Environment}_{DateTime.Now.FormatAsDateForFilePath()}.log");

                if (!Directory.Exists(_workingFolder))
                {
                    Directory.CreateDirectory(_workingFolder);
                }

                if (CheckLogFileInUse(LogFileName))
                {
                    LogFileName = LogFileName.Replace(".log", $"_{Guid.NewGuid().ToString()}.log");
                }

                StartLog();
            }
            catch (Exception exception)
            {
                Trace.TraceError($"Error initializating log file. Exception:\r\n{exception.ToString()}");
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
Пример #2
0
        /// <summary>
        /// Initialises a log file writer and returns the delegate to use.
        /// </summary>
        /// <param name="filename">The filename to write to (not including path).</param>
        /// <returns>An AddEntryDelegate that can be added to Log.OnNewLogEntry.</returns>
        public static AddEntryDelegate WriteToFile(string filename)
        {
            try
            {
                LogFileName = Path.Combine(Application.StartupPath, filename);
                bool append = false;

                if (File.Exists(LogFileName))
                {
                    // get last modified time

                    if (new FileInfo(LogFileName).LastWriteTime > DateTime.Now.AddSeconds(-4))
                    {
                        // modified in last 4 seconds: just transitioned sleep <=> running

                        append = true;
                    }
                    else
                    {
                        // overwrite, but keep previous log

                        string logfilename2 = LogFileName.Replace(".log", ".prev.log");
                        if (File.Exists(logfilename2))
                        {
                            File.Delete(logfilename2);
                        }
                        File.Move(LogFileName, logfilename2);
                    }
                }


                // open log file

                logfile = new StreamWriter(LogFileName, append);
                ((StreamWriter)logfile).AutoFlush = true;


                // write header

                if (!append)
                {
                    logfile.WriteLine("{0} v{1}", Application.ProductName, Application.ProductVersion);
                    logfile.WriteLine(Application.ExecutablePath);
                    logfile.WriteLine("Log created at {0:ddd, dd MMM yyyy HH:mm:ss G\\MTzzz}", DateTime.Now);
                }


                // return AddEntryDelegate

                return(AppendToLogFile);
            }
            catch
            {
                // if error, return null AddEntryDelegate

                logfile = null;
                return(null);
            }
        }