Created
November 11, 2014 02:07
-
-
Save AmrEldib/0bdc92291fd92f9ed336 to your computer and use it in GitHub Desktop.
AppLogger-LoggerClass (Complete)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public AppLogger(string loggerName) | |
{ | |
try | |
{ | |
// Initiate logger object | |
this.Log = LogManager.GetLogger(loggerName); | |
#if DEBUG | |
// Create a log file in the Temp folder | |
string logFilePath = Path.GetTempFileName(); | |
// Add log file path to the configurations stream | |
string loggerConfigurations = | |
Resources.Log4netReleaseConfigurations; | |
loggerConfigurations = | |
loggerConfigurations.Replace(AppLogger._LoggerLocationSymbol, logFilePath); | |
// Convert embedded configurations file to a stream | |
byte[] byteArray = | |
Encoding.ASCII.GetBytes(loggerConfigurations); | |
MemoryStream stream = new MemoryStream(byteArray); | |
// Configure logger from stream | |
XmlConfigurator.Configure(stream); | |
// Store log file path | |
AppLogger.LogFilePath = logFilePath; | |
#endif | |
#if !DEBUG | |
// Create a log file in the Temp folder | |
string logFilePath = Path.GetTempFileName(); | |
// Add log file path to the configurations stream | |
string loggerConfigurations = | |
Resources.Log4netReleaseConfigurations; | |
loggerConfigurations = | |
loggerConfigurations.Replace(AppLogger._LoggerLocationSymbol, logFilePath); | |
// Convert embedded configurations file to a stream | |
byte[] byteArray = | |
Encoding.ASCII.GetBytes(loggerConfigurations); | |
MemoryStream stream = new MemoryStream(byteArray); | |
// Configure logger from stream | |
XmlConfigurator.Configure(stream); | |
// Store log file path | |
AppLogger.LogFilePath = logFilePath; | |
#endif | |
// Supress logger errors/exceptions by default | |
this.SuppressErrors = true; | |
} | |
catch (Exception e) | |
{ | |
// Empty logger object | |
this.Log = null; | |
// Throw exception | |
LoggerInitializationException ex = | |
new LoggerInitializationException("Logger initialization failed", e); | |
throw e; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment