public void WriteToFile(string text, string logFilePath)
        {
            var handleAcquired = false;
            var waitHandleName = "SHARED_BY_ALL_PROCESSES_" + _logFileName;
            var waitHandle     = _eventWaitHandleWrapperProvider.New(true, System.Threading.EventResetMode.AutoReset, waitHandleName);

            try
            {
                var formatted = _textFormatter.FormatTextToLog(text);

                //var formattedWithEndline = formatted + Endline;

                handleAcquired = waitHandle.WaitOne(10000, false);
                var directoryName = Path.GetDirectoryName(logFilePath);
                if (!System.IO.Directory.Exists(directoryName))
                {
                    System.IO.Directory.CreateDirectory(directoryName);
                }
                if (!System.IO.File.Exists(logFilePath))
                {
                    System.IO.File.Create(logFilePath).Close();
                }
                //if (Debugger.IsAttached)
                //    Console.WriteLine(formatted);
                using (var writer = new StreamWriter(logFilePath, true))
                    writer.WriteLine(formatted);
                //writer.WriteLine(formattedWithEndline);
            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            finally
            {
                if (handleAcquired)
                {
                    waitHandle.Set();
                }
            }
            DeleteLogIfTooBigSize(logFilePath);
        }
Exemple #2
0
 public virtual void WriteLine(string logText)
 {
     WriteLineWithoutFormatting(TextLoggerTextFormatter.FormatTextToLog(logText));
 }