Output text to log file

The following method can be used save string messages out to log files.  In the code below I am using a couple class fields to establish the log file name.  These can be customized to meet your need.

 

public void Output(string message) {
        if (_logFilePath != stringEmpty) {
        string fileName = "LogFile";
        if (_contentDatabaseName != string.Empty)
            fileName += "_" + _contentDatabaseName;

        fileName += ".txt";
        string filePath = System.IO.Path.Combine(_logFilePath, fileName);
        System.IO.StreamWriter sw = System.IO.File.AppendText(filePath); // Change filename
        try {
            string logLine = System.String.Format("{0:G}: {1}.", System.DateTime.Now, message);
            sw.WriteLine(logLine);
        } finally {
            sw.Close();
        }
    }
}

Cool eh?

Author

David

comments powered by Disqus

Categories

Recent Tweets

@jeffrey_doucet @Brian_Bancroft Canadians, self define themselves as NOT American, rather than being Proud Canadians
Retweeted by @dyardy Why do so many Canadians obsess over American policy that won’t impact us while being fairly ambivalent to day-to-d… twitter.com/i/web/status/9…
@melrobbins I was looking for 50$ earlier today, and still can't find it...ideas :)
Trump Watches Up To 8 Hours Of TV Per Day | HuffPost #SmartNews ===that explains everything huffingtonpost.com/entry/trump-te…
Retweeted by @dyardy Boston https://t.co/QmtMIF8P0F