Пример #1
0
        public static void Log(string itemToLog, string callerMethodName = null, bool writeToStatusStrip = false)
        {
            if (callerMethodName == null)
            {
                StackTrace stackTrace = new StackTrace();
                MethodBase method     = stackTrace.GetFrame(1).GetMethod();
                callerMethodName = method.Name;
            }

            DateTime date = DateTime.Now;

            LogContents += $"[{callerMethodName} {date}] {itemToLog}\n";

            if (!File.Exists(LogFilePath))
            {
                File.Create(LogFilePath).Close();
            }

            if (File.ReadAllText(LogFilePath) != LogContents)
            {
                File.WriteAllText(LogFilePath, LogContents);
            }

            //Rather than appending text, we'll just check to see if it's different and write everything
            //(There shouldn't ever be so much text that this is a real performance hog and it also has
            //the advantage of taking care of the situation where a user turns on this setting midway
            //through a session of using the program)

            if (writeToStatusStrip)
            {
                UpdateStatusStrip?.Invoke(itemToLog, false, false);
            }
        }
Пример #2
0
        public static void LogError(string itemToLog, string callerMethodName = null, bool writeToStatusStrip = false)
        {
            if (callerMethodName == null)
            {
                StackTrace stackTrace = new StackTrace();
                MethodBase method     = stackTrace.GetFrame(1).GetMethod();
                callerMethodName = method.Name;
            }

            Log(itemToLog, callerMethodName, false); //writeToStatusStrip: false because we're using different parameters and will do it below

            if (writeToStatusStrip)
            {
                UpdateStatusStrip?.Invoke(itemToLog, true, true);
            }
        }