TriEngine  v0.0.16
General-purpose engine in C#/OpenGL
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
TriDevs.TriEngine.Logging.LogManager Class Reference

Class to manage logging. ILog interfaces should be obtained from this class' methods, as opposed to calling default log4net methods. More...

Static Public Member Functions

static void LoadConfig (string file=null)
 Load a config to use with log4net. More...
 
static ILog GetLogger (object sender)
 Gets an ILog object for the specified object. More...
 
static void SetupConsole ()
 Set up a new console for this process. Will not set up a console if a debugger is attached. This method does nothing if DEBUG is not #defined. More...
 
static void DestroyConsole ()
 Destroys the console associated with the process, if loaded. This method does nothing if DEBUG is not #defined. More...
 
static void ClearOldLogs (int daysOld=7, string logsDir="logs")
 Clear logs that are older than the specified amount of days. More...
 

Detailed Description

Class to manage logging. ILog interfaces should be obtained from this class' methods, as opposed to calling default log4net methods.

Member Function Documentation

static void TriDevs.TriEngine.Logging.LogManager.ClearOldLogs ( int  daysOld = 7,
string  logsDir = "logs" 
)
static

Clear logs that are older than the specified amount of days.

Parameters
daysOldLogs older than this amount of days will be deleted.
logsDirThe directory to clear.
136  {
137  var log = GetLogger(typeof(LogManager));
138 
139  log.InfoFormat(">> ClearOldLogs({0}, \"{1}\")", daysOld, logsDir);
140 
141  if (!Directory.Exists(logsDir))
142  {
143  log.InfoFormat("Directory {0} not found, no logs to clear", logsDir);
144  log.Info("<< ClearOldLogs()");
145  return;
146  }
147 
148  var now = DateTime.Now;
149  var max = new TimeSpan(daysOld, 0, 0, 0);
150  var count = 0;
151  foreach (var file in from file in Directory.GetFiles(logsDir)
152  let modTime = File.GetLastAccessTime(file)
153  let age = now.Subtract(modTime)
154  where age > max
155  select file)
156  {
157  try
158  {
159  File.Delete(file);
160  log.InfoFormat("Deleted old log file: {0}", file);
161  count++;
162  }
163  catch (IOException ex)
164  {
165  log.WarnFormat("Failed to delete log file: {0} ({1})", file, ex.Message);
166  }
167  }
168 
169  log.InfoFormat("Done! Cleared {0} log files.", count);
170  log.Info("<< ClearOldLogs()");
171  }
static void TriDevs.TriEngine.Logging.LogManager.DestroyConsole ( )
static

Destroys the console associated with the process, if loaded. This method does nothing if DEBUG is not #defined.

123  {
124 #if DEBUG
125  if (_consoleLoaded)
126  WinAPI.FreeConsole();
127 #endif
128  }
static ILog TriDevs.TriEngine.Logging.LogManager.GetLogger ( object  sender)
static

Gets an ILog object for the specified object.

To get the logger object for a static class, or from static context, call GetLogger(typeof(YourClass)).

Parameters
senderThe object or Type to get an ILog object for.
Returns
The ILog object.
89  {
90  if (!_loaded)
91  LoadConfig();
92 
93  return log4net.LogManager.GetLogger(sender.GetType().ToString() == "System.RuntimeType" ? (Type)sender : sender.GetType());
94  }
static void TriDevs.TriEngine.Logging.LogManager.LoadConfig ( string  file = null)
static

Load a config to use with log4net.

LoadConfig will first try to load the specified file, if not null. If it is unable to find the specified file, it will call itself again with file set to null. If no file is specified, it will attempt to load a config file following the pattern: "(AssemblyName).config" If it is unable to load the config, it will default to BasicConfigurator.

Parameters
fileThe config file to load, null if automatic loading is preferred.
57  {
58  if (file == null)
59  {
60  if (File.Exists(AppDomain.CurrentDomain.FriendlyName + ".config"))
61  XmlConfigurator.Configure();
62  else
63  BasicConfigurator.Configure();
64  }
65  else
66  {
67  if (File.Exists(file))
68  XmlConfigurator.Configure(new FileInfo(file));
69  else
70  {
71  LoadConfig();
72  return;
73  }
74  }
75 
76  _loaded = true;
77  }
static void TriDevs.TriEngine.Logging.LogManager.SetupConsole ( )
static

Set up a new console for this process. Will not set up a console if a debugger is attached. This method does nothing if DEBUG is not #defined.

102  {
103 #if DEBUG
104  if (System.Diagnostics.Debugger.IsAttached)
105  return;
106 
107  WinAPI.AllocConsole();
108  var stdHandle = WinAPI.GetStdHandle(WinAPI.STD_OUTPUT_HANDLE);
109  var safeFileHandle = new SafeFileHandle(stdHandle, true);
110  var fileStream = new FileStream(safeFileHandle, FileAccess.Write);
111  var encoding = Encoding.GetEncoding(WinAPI.CODE_PAGE);
112  var stdOut = new StreamWriter(fileStream, encoding) { AutoFlush = true };
113  Console.SetOut(stdOut);
114  _consoleLoaded = true;
115 #endif
116  }

The documentation for this class was generated from the following file: