1

I have created a simple application to copy files from my computer to a flash drive and I would like to have a log of the user activities. like: The time of starting and closing the application. The path of the copied folder or file and the path of the destination. The time and the name of a newly created folder...etc.

I did not try any thing yet because I have no idea on how to do so.

I need to but the logged data into a text file. And I am using Windows Forms

Thank you in advance.

Zaid_Code36
  • 21
  • 1
  • 5
  • why not just write to a text file? – crthompson Apr 04 '14 at 20:09
  • Then use @safetyOtter's answer. Simple text file writer. There is a problem with concurrency in his solution though. You might want to look into [Log4Net](http://www.nuget.org/packages/log4net/2.0.3). Little complicated to set up, but handles everything you'd ever want. – crthompson Apr 04 '14 at 20:25
  • I did a basic implementation of `a User Activity Logger that hooks up various events`, you can see it here: **http://stackoverflow.com/questions/30326673/user-activity-logging-see-variables-in-a-global-exception-handler** – Jeremy Thompson Jun 01 '15 at 08:12

2 Answers2

1

Using the System.Diagnostics namespace, use the event Log:

Source: http://msdn.microsoft.com/en-us/library/xzwc042w(v=vs.110).aspx

if(!EventLog.SourceExists("MySource"))
    {
         //An event log source should not be created and immediately used. 
         //There is a latency time to enable the source, it should be created 
         //prior to executing the application that uses the source. 
         //Execute this sample a second time to use the new source.
        EventLog.CreateEventSource("MySource", "MyNewLog");
        Console.WriteLine("CreatedEventSource");
        Console.WriteLine("Exiting, execute the application a second time to use the source.");
        // The source is created.  Exit the application to allow it to be registered. 
        return;
    }

EventLog myLog = new EventLog();
    myLog.Source = "MySource";

    // Write an informational entry to the event log.    
    myLog.WriteEntry("Writing to event log.");
T McKeown
  • 12,971
  • 1
  • 25
  • 32
1

I've used something like this for when I want a simple text file log:

    static void LogThis(string logMessage)
    {
        Console.WriteLine(logMessage);
        using (StreamWriter writer = new StreamWriter(FileDate() + ".txt", true))
        {
            writer.WriteLine(logMessage);
            writer.WriteLine();
        }
    }

    static string FileDate()
    {
        return DateTime.Now.ToString("yyyy") + "-" + DateTime.Now.ToString("MM") + "-" + DateTime.Now.ToString("dd");
    }

Usage:

LogThis("I'm logging this!");
safetyOtter
  • 1,430
  • 14
  • 26
  • How do you handle concurrency? – crthompson Apr 04 '14 at 20:24
  • @paqogomez I think just by removing the strings and leaving the function empty. just a guess. – Zaid_Code36 Apr 04 '14 at 20:37
  • @Zaid_Code36, I was asking otter what happens if a multi threaded application uses this bit of code. There is the possibility of the file being locked and an error being thrown. – crthompson Apr 04 '14 at 20:46
  • 1
    @paqogomez If I'm in a multi threaded application, I use the event logging. If I'm in a quick and dirty utility, app, I use this. – safetyOtter Apr 04 '14 at 21:48
  • @Zaid_Code36 the `string logMessage` is the text you want to log, in your forms loaded event call `LogThis("App started");` or whatever. paqogome is right though, if this is a multi threaded app or if you want more full featured text logging you should check out Log4Net – safetyOtter Apr 04 '14 at 22:02
  • Agreed on concurrency being a complete non-issue here. However, why not format the date in a single call? `DateTime.Now.ToString("yyyy-MM-dd")` will give the same result but be faster and immune to weird edge cases. It also makes the FileDate() function largely unnecessary. – Nick Apr 04 '14 at 22:17
  • @Nick good points. Thank you. I think it's the way it is because It used to be something else and got refactored down but not far enough. – safetyOtter Apr 04 '14 at 22:21