1

I'm creating a folder monitor that scans a folder for incomming xml files. When a xml file has been created in the folder, the file will be parsed, and after parsing it'll be moved to a "processed" folder. Now there are a lot of things that can go wrong obviously, and I'd like to be able to display a list of errors in this fasion:

Filename 1 >> error

Filename 2 >> error

But I realy dont know where to start. should I make a dictionary with string/string pair and display it in a tabel? Or what is the best option here. I tried googling, but it's hard to find examples of this.

thanks in advance.

Rob
  • 151
  • 2
  • 4
  • 13
  • Use something like from this answer: http://stackoverflow.com/questions/252323/how-do-i-add-a-console-like-element-to-a-c-sharp-winforms-program – Steve Jun 07 '13 at 12:53
  • One thing to watch out with monitoring folders is to ensure you do not try to read a file that is still being written. This can easily happen if the file is created for writing with its final file name. A better design is to open files with temporary names, e.g. myFile.tmp, write your data to it, close it, then rename myFile.tmp to myFile.xml or whatever. Your monitor must ignore .tmp files. – Dour High Arch Jun 07 '13 at 15:55

3 Answers3

1

Well as you said you could use a Dictionary then put in a data table and then probably bind it to a DataGrid like:

        Dictionary<String, String> dict = new Dictionary<String,String>();
        dict.Add("Filename1","Error1");
        dict.Add("Filename2","Error2");
        dict.Add("Filename3","Error3");
        DataTable table = new DataTable();

        table.Columns.Add("Filename", typeof(String));
        table.Columns.Add("Error_Description", typeof(String));

        foreach (KeyValuePair<String,String> dictval in dict)
            {
                table.Rows.Add(dictval.Key, dictval.Value);
            }
        dataGridView1.DataSource = table;
Edper
  • 9,144
  • 1
  • 27
  • 46
1

You can use Enterprise Library for logging or create your own Logger class like this :

public static class Logger
{
    public static List<Error> Logs = new List<Error>();

    public static void Log(Exception ex,string fileName)
    {
        Logs.Add(new Error
        {
            Message = ex.Message,
            FileName = fileName
        });
        //Here you can log errors to database,txt or xml too.
    }
}

public class Error
{
    public string Message { get; set; }
    public string FileName { get; set; }
}

And use your logger in FileSystemWatcher class' Created event like this :

void watcher_Created(object sender, FileSystemEventArgs e)
    {
        try
        {
            //Your logic
        }
        catch (Exception ex)
        {
            Logger.Log(ex, e.Name);
            //To show your logs in grid
            dataGridView.DataSource = null;
            dataGridView.DataSource = Logger.Logs;
        }
    }
SerkanOzvatan
  • 231
  • 1
  • 5
0

You really should consider using something like log4net to do the logging. It can be downloaded here. And the configuration is dead simple. In your app.config file you can add the section tag to the configSections tag and then add the log4net configuration tag.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="EventLogAppender" />
    </root>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <threshold value="DEBUG" />
      <applicationName value="Lantic YCS WebServer" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="(%-5level %date{hh}:%date{mm}:%date{ss} [%thread] %logger [%property{NDC}] ) %message %n" />
      </layout>
    </appender>
  </log4net>

</configuration>

Then in your code you just need to initialize it, so at the top of every class file drop in this line:

private static readonly ILog log = LogManager.GetLogger(typeof(Global));

allowing you to say something like:

log.Error(...);

but also, when the application first starts, don't forget to run this line:

log4net.Config.XmlConfigurator.Configure();

Now, to address a more interesting concern that I personally have. You stated that you're building a folder monitor. You really don't want, or need, to build that. Please simply use the FileSystemWatcher that's already available. It's extremely efficient, and filterable as well, so you get the messages you want.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232