uwekeim icon

The shortest possible file logging class

uwekeim | PRO | 08/27/14 02:49:57 PM UTC | 0 ⭐ | 757 👁️ | Never ⏰ | []
C# |

1.1 KB

|

None

|

0 👍

/

0 👎

namespace Helper
{
    // http://www.zeta-producer.com
 
    using System;
    using System.IO;
    using System.Reflection;
 
    /// <summary>
    /// The shortest possible logger.
    /// </summary>
    public static class QuickLogger
    {
        private static readonly Lazy<string> LogFilePath = new Lazy<string>(() =>
        {
            var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var fileName = Path.ChangeExtension(Path.GetFileName(Assembly.GetEntryAssembly().Location), @".log");
 
            return Path.Combine(folderPath, fileName);
        });
 
        public static void Log(string text, params object[] args)
        {
            if (!string.IsNullOrEmpty(text))
            {
                var msg = string.Format(@"[{0}, {1}\{2}] {3}" + Environment.NewLine,
                    DateTime.Now,
                    Environment.UserDomainName,
                    Environment.UserName,
                    string.Format(text, args));
 
                File.AppendAllText(LogFilePath.Value, msg);
            }
        }
    }
}

Comments