Skip to content

Instantly share code, notes, and snippets.

@AmrEldib
Created November 11, 2014 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmrEldib/0bdc92291fd92f9ed336 to your computer and use it in GitHub Desktop.
Save AmrEldib/0bdc92291fd92f9ed336 to your computer and use it in GitHub Desktop.
AppLogger-LoggerClass (Complete)
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