Create a LogManager static class in your framework.
Logger, LogEntry, ShouldLog are all provided by EntLib
LogCategory is an enum which can describe from which layer this log was generated.
{
public static void LogMessage(LogCategory category, LogPriority priority, string message)
{
string eventCategory = category.ToString();
LogEntry le = new LogEntry();
le.Priority = (int)priority;
le.Categories.Add(eventCategory);
if (Logger.ShouldLog(le))
{
le.TimeStamp = DateTime.Now;
le.Message = message;
le.ExtendedProperties.Add("Event Source:", eventCategory);
if (category == LogCategory.SystemErrorTrace || category == LogCategory.FatalErrorTrace)
{
le.Severity = System.Diagnostics.TraceEventType.Error;
}
try
{
Logger.Write(le);
}
catch (Exception)
{
}
}
}
}
Logger, LogEntry, ShouldLog are all provided by EntLib
LogCategory is an enum which can describe from which layer this log was generated.
public static class LogManager
{
public static void LogMessage(LogCategory category, LogPriority priority, string message)
{
string eventCategory = category.ToString();
LogEntry le = new LogEntry();
le.Priority = (int)priority;
le.Categories.Add(eventCategory);
if (Logger.ShouldLog(le))
{
le.TimeStamp = DateTime.Now;
le.Message = message;
le.ExtendedProperties.Add("Event Source:", eventCategory);
if (category == LogCategory.SystemErrorTrace || category == LogCategory.FatalErrorTrace)
{
le.Severity = System.Diagnostics.TraceEventType.Error;
}
try
{
Logger.Write(le);
}
catch (Exception)
{
}
}
}
}
If you need to log an entity as XML:
public static void LogEntity(LogCategory category, LogPriority priority, object valueToLog, string logMessage)
{
string eventCategory = category.ToString();
LogEntry le = new LogEntry();
le.Priority = (int)priority;
le.Categories.Add(eventCategory);
if (Logger.ShouldLog(le))
{
using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
{
if (valueToLog != null)
{
try
{
stringWriter.WriteLine(logMessage);
// Serailize the input object to stream
XmlSerializer xmlSerializer = new XmlSerializer(valueToLog.GetType());
xmlSerializer.Serialize(stringWriter, valueToLog);
}
catch (Exception ex)
{
stringWriter.WriteLine("Something wrong"+ex.Message);
}
}
else
{
stringWriter.WriteLine("Entity object = null");
}
try
{
le.TimeStamp = DateTime.Now;
le.ExtendedProperties.Add("Event Source:", eventCategory);
le.Message = stringWriter.ToString();
if (category == LogCategory.SystemErrorTrace || category == LogCategory.FatalErrorTrace)
{
le.Severity = System.Diagnostics.TraceEventType.Error;
}
Logger.Write(le);
}
catch (Exception)
{
}
}
}
}
How Logging disabling works:
Below is snippet from web.config file:
<logFilters>
<add categoryFilterMode="AllowAllExceptDenied" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=7fa4cc4c62ff1809"
name="Category Filter">
<categoryFilters>
<!--<add name="SourceTrace" />
<add name="ManagerTrace" />
<add name="ProviderTrace" />
<add name="InformationTrace" />
<add name="ServiceTrace" />-->
</categoryFilters>
</add>
</logFilters>
<categorySources>
..
</categorySources>
Logging will be disabled only when a category filter is commented. tracingEnabled="false" does not work.
No comments :
Post a Comment