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

19 Types of Developers Explained dev.to/lpasqualis/19-…
Can't Afford Photoshop? Here Is The Best Free Drawing Software | Digital Trends digitaltrends.com/computing/best…
Retweeted by @dyardy Fun day christening Amazon’s latest wind farm. #RenewableEnergy https://t.co/cTxeXdsFop
Retweeted by @dyardy Hey C# peeps, we’re trying C# in browser with reference content. Try it here: docs.microsoft.com/dotnet/csharp/… reply with feedback /cc @LadyNaggaga
23 Amazing Vintage Photographs Taken Inside WWII Tank Factories ~ vintage everyday vintag.es/2017/10/23-ama…