using System; using System.Diagnostics; using iiie.Logs.DBO; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.Data; using Microsoft.Practices.EnterpriseLibrary.Logging; namespace iiie.Logs.DataAccess { /// /// Logger for OpResult /// public static class Logger { /// /// /// /// /// public static TraceEventType ResultStatusToSeverity(ResultStatus status) { if (status == ResultStatus.DBError) return TraceEventType.Critical; if (status == ResultStatus.LoginError) return TraceEventType.Error; if (status == ResultStatus.PermissionError) return TraceEventType.Error; if (status == ResultStatus.InternalError) return TraceEventType.Error; if (status == ResultStatus.InputError) return TraceEventType.Warning; if (status == ResultStatus.NotFound) return TraceEventType.Information; return TraceEventType.Verbose; } /// /// Config the logger for database and default settings provider /// public static void Config() { DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory()); IConfigurationSource configurationSource = ConfigurationSourceFactory.Create(); LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource); Microsoft.Practices.EnterpriseLibrary.Logging.Logger.SetLogWriter(logWriterFactory.Create()); } /// /// Log provided informations /// /// The status to be logged /// The log title /// The log message public static void Log(ResultStatus status, string title, string message) { try { Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(new LogEntry() { Title = title, Severity = ResultStatusToSeverity(status), Message = message }); } catch (Exception e) { } } /// /// Log an exception /// /// The exception to be logged public static void Log(Exception e) { LogOpResult(OpResult.Error(ResultStatus.InternalError, e)); } /// /// Log an OpResult /// /// User data type /// The result to be logged /// The OpResult public static OpResult LogOpResult(OpResult result) { string message = "Location:\n"; for (int i = 0; i < result.StackFrames.Length && i < 10; ++i) { var frame = result.StackFrames[i]; var method = frame.GetMethod(); if (method.DeclaringType != null) message += method.DeclaringType.Name + "." + frame.GetMethod().Name + "\n"; } if (result.PrivateDetails != null) message += "\nPrivate details:\n" + result.PrivateDetails + "\n"; if (result.PublicDetails != null) message += "\nPublic details:\n" + result.PublicDetails; Log(result.Status, result.Status.ToString(), message); return result; } /// /// Create and returns a new OpResult with /// PrivateDetails = privateDetails, Data = null, Status = status /// If publicDetails == "", privateDetails will be used /// Used for all errors (invalid input, ...) /// public static OpResult Error(ResultStatus status, string privateDetails, string publicDetails = null) { return LogOpResult(OpResult.Error(status, privateDetails, publicDetails)); } /// /// Create and returns a new OpResult with /// PrivateDetails = e to string, Data = null, Status = status /// If publicDetails == "", privateDetails will be used /// Used for all errors (invalid input, ...) /// public static OpResult Error(ResultStatus status, Exception e, string userDetails = null) { return LogOpResult(OpResult.Error(status, e, userDetails)); } public static OpResult Log(this OpResult res) { return LogOpResult(res); } } }