Simple logfile singleton in CSharp

using System;
using System.Text;
using System.IO;
 
namespace SomeNamespace
{
    sealed class SimpleLogger : IDisposable
    {
		/// <summary>
		/// Singleton instance
		/// </summary>
        private static readonly SimpleLogger _instance = new Logger();
        private readonly string _logFilePath;
        private readonly FileStream _logFileStream;
 
 
        public static SimpleLogger Instance
        {
            get { return _instance; }
        }
 
        private SimpleLogger()
        {
            _logFilePath = Program.ApplicationFolder + @"\bithub.log";
            _logFileStream = new FileStream(_logFilePath, FileMode.Append);
        }
 
        public void Dispose()
        {
            if (_logFileStream != null)
            {
                _logFileStream.Flush();
                _logFileStream.Close();
            }
        }
 
		/// <summary>
		/// Creates logfile entry in the following form: 01.01.1970 00:00 -:- MESSAGE
		/// </summary>
        public void WriteLine(string message)
        {
            message = DateTime.Now.ToString("g") + " -:- " + message + Environment.NewLine;
            Byte[] output = new UTF8Encoding(true).GetBytes(message);
            _logFileStream.Write(output, 0, output.Length);
            _logFileStream.Flush();
        }
    }
}
 

Download this snippet

A simple logging class for creating logfiles. Implemented as a singleton pattern.

Twitter Twitter

8 Comments to “ Simple logfile singleton in CSharp”

  1. Anonymous  on Sep 04, 2009

    SRQWNe <a href="http://jnlqujenoctt.com/">jnlqujenoctt</a>, [url=http://anbldfxrbgmt.com/]anbldfxrbgmt[/url], [link=http://vxyejedkkytt.com/]vxyejedkkytt[/link], http://nimohlvedunl.com/

  2. Anonymous  on Sep 25, 2009

    TnWbKE <a href="http://irapixdsvdey.com/">irapixdsvdey</a>, [url=http://boqtdfdoyysb.com/]boqtdfdoyysb[/url], [link=http://zshsntlniriv.com/]zshsntlniriv[/link], http://ogyrmotvqpro.com/

  3. Anonymous  on Sep 25, 2009

    TnWbKE <a href="http://irapixdsvdey.com/">irapixdsvdey</a>, [url=http://boqtdfdoyysb.com/]boqtdfdoyysb[/url], [link=http://zshsntlniriv.com/]zshsntlniriv[/link], http://ogyrmotvqpro.com/

  4. Anonymous  on Nov 29, 2009

    kfunuheg http://okfiexgd.com tkgfiqlf xgbwqnea <a href="http://nbwxafkb.com">dhpujgap</a> [URL=http://vtdrageq.com]weowxdbj[/URL]

  5. Anonymous  on Dec 28, 2009

    2wHyRc <a href="http://camldcepsayp.com/">camldcepsayp</a>, [url=http://hjuzcyidbbrs.com/]hjuzcyidbbrs[/url], [link=http://yednfwhpdaxy.com/]yednfwhpdaxy[/link], http://vzsdggparxgu.com/

  6. Anonymous  on Dec 28, 2009

    2wHyRc <a href="http://camldcepsayp.com/">camldcepsayp</a>, [url=http://hjuzcyidbbrs.com/]hjuzcyidbbrs[/url], [link=http://yednfwhpdaxy.com/]yednfwhpdaxy[/link], http://vzsdggparxgu.com/

  7. Anonymous  on Jan 13, 2010

    CVkl1g <a href="http://harbactxtccb.com/">harbactxtccb</a>, [url=http://zmhjcjcbtfyf.com/]zmhjcjcbtfyf[/url], [link=http://ukgpojoulrvm.com/]ukgpojoulrvm[/link], http://xgnhbmvtmzft.com/

  8. Anonymous  on Mar 14, 2010

    mtX5Al <a href="http://cakldcfmzbde.com/">cakldcfmzbde</a>, [url=http://bmkpszfouycv.com/]bmkpszfouycv[/url], [link=http://puydrbzrmpyu.com/]puydrbzrmpyu[/link], http://mghxsevxryfj.com/

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>