There are plenty of logging systems about for .NET. This site although it’s an advert for a .NET logging system has links and comparisons with many.  But sometimes all you need is a very simple logger, something as simple as appending a string to a text file. So here’s a 64 line logger that creates a dailyfile – for today it’s 20131004.log.

using System;
using System.IO;
using System.Threading.Tasks;

namespace TMRTaxi
{
    class Logger : IDisposable
    {
        public string FileName { get; set; }
        private StreamWriter sw;

        public Logger()
        {
            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var cache = Path.Combine(documents, "..", "Library", "Caches");
            var logfile = Path.Combine(cache, @"log" + DateTime.Now.ToString("yyyymmdd") + ".log");
            FileName = logfile;
            var str = "Logging Startedn";

            sw = new StreamWriter(FileName, true);
            Log(str);

        }

        public void Pause()
        {
            if (sw != null)
            {
                sw.Flush();
                sw.Close();
                sw = null;
            }            
        }

        public void Log(string msg,string comment="")
        {
            if (sw == null)
                return;
            msg = DateTime.Now.ToString("T") + " : "+msg+(comment != ""?" ["+comment+"]":"");
            Task.Factory.StartNew(()=> sw.WriteLine(msg), TaskCreationOptions.LongRunning);           
        }

        public void Dispose(Boolean disposing)
        {
            if (sw != null)
            {
                sw.Close();
                sw = null;
            }    
        }

        public void Dispose()
        {
            Dispose(true); //i am calling you from Dispose, it's safe
            GC.SuppressFinalize(this); //Hey, GC: don't bother calling finalize later
        }

        ~Logger()
        {
            Dispose(false); //i am *not* calling you from Dispose, it's *not* safe
        }
    }
}

It could probably do with a bit of extra error checking. A try catch round the sw = new StreamWriter(FileName, true); line for instance.

These lines:

var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var cache = Path.Combine(documents, "..", "Library", "Caches");
            var logfile = Path.Combine(cache, @"log" + DateTime.Now.ToString("yyyymmdd") + ".log");

create the log file in the caches folder.

This line Task.Factory.StartNew(()=> sw.WriteLine(msg), TaskCreationOptions.LongRunning); in Log() does a fire and forget. It runs it using the TPL (Task Parallel library). It seems to work fine.

Just creature a logger with

log = new Logger();
log.Log("App starting up");

Plus there’s an optional comment on the Log method.

Of course it would be nice if you can email the logs to yourself but programmatically that’s a no no. “Apfel verbietet es” as my German teacher might have said. (“Apple forbids it.”).

The workaround is to build a large text file from all the available log files (and remove them afterwards) then post it to a php script on a website and it emails it. I’ll post the C# code for that in a day or two.

Here’s proof- well it works in the simulator anyway!

ios-simulator

Leave a Reply

Post Navigation