Advertisement
uwekeim

The shortest possible file logging class

Aug 27th, 2014
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. namespace Helper
  2. {
  3.     // http://www.zeta-producer.com
  4.  
  5.     using System;
  6.     using System.IO;
  7.     using System.Reflection;
  8.  
  9.     /// <summary>
  10.     /// The shortest possible logger.
  11.     /// </summary>
  12.     public static class QuickLogger
  13.     {
  14.         private static readonly Lazy<string> LogFilePath = new Lazy<string>(() =>
  15.         {
  16.             var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  17.             var fileName = Path.ChangeExtension(Path.GetFileName(Assembly.GetEntryAssembly().Location), @".log");
  18.  
  19.             return Path.Combine(folderPath, fileName);
  20.         });
  21.  
  22.         public static void Log(string text, params object[] args)
  23.         {
  24.             if (!string.IsNullOrEmpty(text))
  25.             {
  26.                 var msg = string.Format(@"[{0}, {1}\{2}] {3}" + Environment.NewLine,
  27.                     DateTime.Now,
  28.                     Environment.UserDomainName,
  29.                     Environment.UserName,
  30.                     string.Format(text, args));
  31.  
  32.                 File.AppendAllText(LogFilePath.Value, msg);
  33.             }
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement