Exemplo n.º 1
0
        /// <summary>
        /// Generates a log entry, formats it into a readable string and
        /// writes it to the configured LoggerProvider.  The LoggerProvider
        /// is pluggable and managed by the LoggerManager class.
        /// </summary>
        /// <param name="logLevel">The level of the entry to be writen</param>
        /// <param name="message">The message to be written</param>
        /// <param name="exception">The exception to be documented</param>
        /// <param name="properties">A collection of custom parameters</param>
        public void WriteLogEntry(LogLevel logLevel, string message, Exception exception, IDictionary <string, object> properties)
        {
            //TODO:  ADD ADDITIONAL WRITE METHODS

            // Do not write the log entry if the current level is
            // less than than the configured level
            if (logLevel < LoggerManager.Instance.Level)
            {
                return;
            }

            ExceptionData exceptionData = null;

            // If an exception was provided, gather data from it
            if (exception != null)
            {
                exceptionData = new ExceptionData(exception);
            }

            // Generate client data
            ClientData clientData = new ClientData()
            {
                // We are currently limited to the client
                // information that we can collect from
                // Silverlight.
                LogName = this.name,
                Url     = PageUri.ToString()
            };

            // Create the actual LogEntry using all provided parameters and
            // all generated information
            LogEntry logEntry = new LogEntry()
            {
                ExceptionInfo   = exceptionData,
                ClientInfo      = clientData,
                LogLevel        = logLevel,
                Message         = message,
                TypeName        = callerType != null ? callerType.FullName : string.Empty,
                ThreadName      = System.Threading.Thread.CurrentThread.Name,
                ManagedThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId,
                OccuredAt       = DateTime.Now,
                Properties      = properties != null ? new Dictionary <string, object>(properties) : null
            };

            // Construct the message so the provider is only
            // responsible for writing the string
            StringBuilder logMessage = new StringBuilder();

            logMessage.AppendLine(string.Format("[{0,-4}] {1:s} {2,5} <{3}> {4}", logEntry.ManagedThreadId, logEntry.OccuredAt, logEntry.LogLevel.ToString(), logEntry.TypeName, logEntry.Message));

            // Write any extended properties
            if (logEntry.Properties != null)
            {
                logMessage.Append(string.Format("[{0,-4}] ->", "ADDL PROPS"));
                foreach (KeyValuePair <string, object> property in logEntry.Properties)
                {
                    logMessage.Append(string.Format("{0} = {1};", property.Key, property.Value.ToString()));
                }
            }

            // Write any exception information
            if (logEntry.ExceptionInfo != null)
            {
                logMessage.Append(string.Format("[{0,-4}] ->", "EXCEPTION INFO"));
                logMessage.AppendLine(logEntry.ExceptionInfo.ToString());
            }

            // Instruct the provider to physically write the log entry
            //LoggerManager.Instance.LoggerProvider.Write(logMessage.ToString());
        }
 public override string ToString()
 {
     return(PageUri.ToString());
 }