Class to manage logging. ILog interfaces should be obtained from this class' methods, as opposed to calling default log4net methods.
More...
|
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...
|
|
Class to manage logging. ILog interfaces should be obtained from this class' methods, as opposed to calling default log4net methods.
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
-
daysOld | Logs older than this amount of days will be deleted. |
logsDir | The directory to clear. |
139 log.InfoFormat(
">> ClearOldLogs({0}, \"{1}\")", daysOld, logsDir);
141 if (!Directory.Exists(logsDir))
143 log.InfoFormat(
"Directory {0} not found, no logs to clear", logsDir);
144 log.Info(
"<< ClearOldLogs()");
148 var now = DateTime.Now;
149 var max =
new TimeSpan(daysOld, 0, 0, 0);
151 foreach (var file
in from file
in Directory.GetFiles(logsDir)
152 let modTime = File.GetLastAccessTime(file)
153 let age = now.Subtract(modTime)
160 log.InfoFormat(
"Deleted old log file: {0}", file);
163 catch (IOException ex)
165 log.WarnFormat(
"Failed to delete log file: {0} ({1})", file, ex.Message);
169 log.InfoFormat(
"Done! Cleared {0} log files.", count);
170 log.Info(
"<< ClearOldLogs()");
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.
126 WinAPI.FreeConsole();
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
-
sender | The object or Type to get an ILog object for. |
- Returns
- The ILog object.
93 return log4net.LogManager.GetLogger(sender.GetType().ToString() ==
"System.RuntimeType" ? (Type)sender : sender.GetType());
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
-
file | The config file to load, null if automatic loading is preferred. |
60 if (File.Exists(AppDomain.CurrentDomain.FriendlyName +
".config"))
61 XmlConfigurator.Configure();
63 BasicConfigurator.Configure();
67 if (File.Exists(file))
68 XmlConfigurator.Configure(
new FileInfo(file));
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.
104 if (System.Diagnostics.Debugger.IsAttached)
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;
The documentation for this class was generated from the following file: