-3

I have a windows application where I do have some functionalities like Find and Replace, changing the date formats and so on.

By generating the LOG file, I would like to trace the modifications done to the file in my windows application.

Can anyone help me achieving this?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 2
    What is the actual question? How to write to a log file? Try one of the many logging libraries like NLog, log4net etc. How to track changes made by commands in your application? Write to the log when the command starts and completes. Something else? Please make a concrete question – Panagiotis Kanavos Feb 29 '16 at 11:01
  • 1
    Try looking here: [How to do logging in c#?](http://stackoverflow.com/questions/5057567/how-to-do-logging-in-c) – Pep_8_Guardiola Feb 29 '16 at 11:05
  • I want to track the changes of the file in my application. – user1191818 Feb 29 '16 at 11:07

2 Answers2

2

If you want to Log actions performed by application you can write them to an external file.

private void Log(string text, bool LineBreak)
    {
        try
        {
            if (LineBreak)
            {
                File.AppendAllText("LogFile.txt", System.Environment.NewLine + System.Environment.NewLine);
            }
            File.AppendAllText("LogFile.txt", System.Environment.NewLine + DateTime.Now + ":- " + text);
        }
        catch
        {
        }
    }

Log exceptions like this:

private void LogException(Exception ex)
    {
        try
        {
            File.AppendAllText("LogFile.txt", System.Environment.NewLine + DateTime.Now + ":- Main Exception:");
            File.AppendAllText("LogFile.txt", System.Environment.NewLine + ex.Message);
            File.AppendAllText("LogFile.txt", System.Environment.NewLine + ex.StackTrace);

            File.AppendAllText("LogFile.txt", System.Environment.NewLine + System.Environment.NewLine + "Inner Exception:");
            File.AppendAllText("LogFile.txt", System.Environment.NewLine + ex.InnerException.Message);
            File.AppendAllText("LogFile.txt", System.Environment.NewLine + ex.InnerException.StackTrace);
        }
        catch
        {
        }
    }
Zain
  • 272
  • 2
  • 11
  • Can you please tell me what's the assembly need to be added for ServiceLogFilePath? – user1191818 Feb 29 '16 at 13:43
  • @user1191818 "ServiceLogFilePath" is a variable contain log file path . see i have edited code spinet for clarification. – Zain Feb 29 '16 at 14:01
0

It would be possible to do some logging to the trace output via

Trace.WriteLine("Some message");

and hook up a trace listener, which implements

TraceListener

or use a predifined listener. A comprehensive tutorial can be found on MSDN.

Codor
  • 17,447
  • 9
  • 29
  • 56