SQL WriteLog Severity Level

Topics: Logging Application Block
Mar 25, 2014 at 5:15 PM
Edited Mar 25, 2014 at 6:54 PM
I have some issue getting my exception entry logged with the correct severity as "Error". The code I have always created the entry with "Information".

C# Routine
        public static void LogSqlException(string spName, int errCode, string errDescription, string codeLocator)
        {
            var entry = new LogEntry();
            entry.Message = string.Format("SP Name:{0}, Error Code:{1}, Error Description:{2}, Code Locator:{3}", spName,
                errCode, errDescription, codeLocator);
            entry.Severity = TraceEventType.Error;
            entry.Priority = 3; //Default MPD priority is 3
            LogError(entry);
        }

        private static void LogError(LogEntry entry)
        {
            string sqlConnectionString = EnvironmentHelper.GetSqlConnectionString();
            if (sqlConnectionString == null) throw new ArgumentNullException(sqlConnectionString + " is null");
            ConfigureContainer("LoggingDB", sqlConnectionString);
            Logger.Write(entry, CategorySource.Exceptions.ToString());
        }
This is my exceptionHandlingConfig section
 <!--Exception Handling config-->
  <exceptionHandling>
    <exceptionPolicies>
      <add name="Exception Logging Policy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="Exceptions" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                priority="3" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
Mar 27, 2014 at 1:34 PM
The reason why you are seeing the severity "Information" is because you are calling the overload of Logger.Write that accepts an object and a string instead of the Logger.Write(LogEntry) overload. In the former case, a new LogEntry will be created with a Severity of Information.

You can change the code to:
            var entry = new LogEntry()
            {
                Message = string.Format("SP Name:{0}, Error Code:{1}, Error Description:{2}, Code Locator:{3}", spName,
                errCode, errDescription, codeLocator),
                Severity = TraceEventType.Error,
                Priority = 3
            };

            entry.Categories.Add(CategorySource.Exceptions.ToString());

            Logger.Write(entry);
I would not recommend configuring the block on every write call. That would be a performance hit and you would also have to ensure thread safety. If the connection string does not change then I would recommend configuring once at application startup. If you wish to support multiple connection strings then I would create a dictionary of LogWriter's -- one for each database -- and retrieve the correct LogWriter.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 27, 2014 at 4:15 PM

That was it! Thanks Randy. Your tips on not configuring the DB on every call is a life saver.