protected override void WriteInternal(LogLevel level, object message, Exception exception)
{
String msgText = null;
if (message == null)
msgText = "";
else if (message is string)
msgText = (String)message;
else
{
var conv = TypeDescriptor.GetConverter(message.GetType());
if (conv.CanConvertTo(typeof(string)))
msgText = (string)conv.ConvertTo(message, typeof(string));
}
var le = new LogEvent
{
LoggerName = _loggerName,
Level = level,
Message = msgText,
ExceptionStackTrace = exception != null ? exception.ToString() : null,
CreatedDate = DateTime.UtcNow
};
try
{
le.ThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(null as IFormatProvider);
le.DeploymentId = RoleEnvironment.IsAvailable ? RoleEnvironment.DeploymentId : "N/A";
le.RoleName = RoleEnvironment.IsAvailable ? RoleEnvironment.CurrentRoleInstance.Role.Name : "N/A";
}
catch (Exception exLog) //lack of ability to load these should not prevent logging from occuring.
{
if (le.ExceptionStackTrace == null)
le.ExceptionStackTrace = "No Exception in event, but the following occured while logging the event.";
le.ExceptionStackTrace += String.Format(" {0}{1}", Environment.NewLine, exLog);
}
TableProxy.Insert(le);
}