To use the Serializable log entry,
Pass this to the Webservice(new SerializableLogEntry(logEntry));
using System;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Collections;
namespace Infrastructure.Logging.Providers
{
/// <summary>
/// SerializableLogEntry
/// </summary>
[Serializable]
public class SerializableLogEntry
{
private int eventId = 0;
private int priority = -1;
private string severity;
private string title =
string.Empty;
private string message =
string.Empty;
private List<
string> categories =
new List<
string>(0);
private string machineName =
string.Empty;
private DateTime timeStamp = DateTime.MaxValue;
private string errorMessages;
private Dictionary<
string,
string> extendedProperties;
private string appDomainName;
private string processId;
private string processName;
private string threadName;
private string win32ThreadId;
/// <summary>
/// Initialize a new instance of a <see cref="LogEntry"/> class.
/// </summary>
public SerializableLogEntry()
{ }
/// <summary>
/// Initializes a new instance of the <see cref="SerializableLogEntry"/>
class.
/// </summary>
/// <param name="logEntry">The log entry.</param>
public SerializableLogEntry(LogEntry logEntry)
{
Message
= logEntry.Message;
EventId
= logEntry.EventId;
Title
= logEntry.Title;
Severity
= logEntry.Severity.ToString();
Priority
= logEntry.Priority;
TimeStamp
= logEntry.TimeStamp;
MachineName
= logEntry.MachineName;
AppDomainName
= logEntry.AppDomainName;
ProcessId
= logEntry.ProcessId;
ProcessName
= logEntry.ProcessName;
ManagedThreadName
= logEntry.ManagedThreadName;
Win32ThreadId
= logEntry.Win32ThreadId;
ErrorMessages
= logEntry.ErrorMessages;
Categories.AddRange(logEntry.Categories
as IEnumerable<
string>);
ExtendedProperties
= logEntry.ExtendedProperties
as Dictionary<
string,
string>;
}
/// <summary>
/// Message body to log. Value from ToString() method from message object.
/// </summary>
/// <value>The message.</value>
public string Message
{
get
{
return this.message; }
set
{
this.message =
value; }
}
/// <summary>
/// Category name used to route the log entry to a one or more trace listeners.
/// </summary>
/// <value>The categories.</value>
public List<
string> Categories
{
get
{
return categories; }
set
{
this.categories =
value; }
}
/// <summary>
/// Importance of the log message. Only messages whose priority is between
the minimum and maximum priorities (inclusive)
/// will be processed.
/// </summary>
/// <value>The priority.</value>
public int Priority
{
get
{
return this.priority; }
set
{
this.priority =
value; }
}
/// <summary>
/// Event number or identifier.
/// </summary>
/// <value>The event id.</value>
public int EventId
{
get
{
return this.eventId; }
set
{
this.eventId =
value; }
}
/// <summary>
/// Log entry severity as a <see cref="Severity"/> enumeration. (Unspecified,
Information, Warning or Error).
/// </summary>
/// <value>The severity.</value>
public string Severity
{
get
{
return this.severity; }
set
{
this.severity =
value; }
}
/// <summary>
/// Additional description of the log entry message.
/// </summary>
/// <value>The title.</value>
public string Title
{
get
{
return this.title; }
set
{
this.title =
value; }
}
/// <summary>
/// Date and time of the log entry message.
/// </summary>
/// <value>The time stamp.</value>
public DateTime TimeStamp
{
get
{
return this.timeStamp; }
set
{
this.timeStamp =
value; }
}
/// <summary>
/// Name of the computer.
/// </summary>
/// <value>The name of the machine.</value>
public string MachineName
{
get
{
return this.machineName; }
set
{
this.machineName =
value; }
}
/// <summary>
/// The <see cref="AppDomain"/> in which the program is running
/// </summary>
/// <value>The name of the app domain.</value>
public string AppDomainName
{
get
{
return this.appDomainName; }
set
{
this.appDomainName =
value; }
}
/// <summary>
/// The Win32 process ID for the current running process.
/// </summary>
/// <value>The process id.</value>
public string ProcessId
{
get
{
return processId; }
set
{ processId =
value; }
}
/// <summary>
/// The name of the current running process.
/// </summary>
/// <value>The name of the process.</value>
public string ProcessName
{
get
{
return processName; }
set
{ processName =
value; }
}
/// <summary>
/// The name of the .NET thread.
/// </summary>
/// <value>The name of the managed thread.</value>
/// <seealso cref="Win32ThreadId"/>
public string ManagedThreadName
{
get
{
return threadName; }
set
{ threadName =
value; }
}
/// <summary>
/// The Win32 Thread ID for the current thread.
/// </summary>
/// <value>The win32 thread id.</value>
public string Win32ThreadId
{
get
{
return win32ThreadId; }
set
{ win32ThreadId =
value; }
}
///// <summary>
///// Dictionary of key/value pairs to record.
///// </summary>
/// <summary>
/// Gets or sets the extended properties.
/// </summary>
/// <value>The extended properties.</value>
[XmlIgnore]
public Dictionary<
string,
string> ExtendedProperties
{
get
{
if (extendedProperties ==
null)
{
extendedProperties
=
new Dictionary<
string,
string>();
}
return this.extendedProperties;
}
set
{
this.extendedProperties =
value; }
}
//<summary>
//Property Created explicitly for XMLSerializer
//</summary>
//<remarks>
//</remarks>
/// <summary>
/// Gets or sets the _entry_sort expressions.
/// </summary>
/// <value>The _entry_sort expressions.</value>
[XmlArray(
"Array")]
[XmlArrayItem(
"ArrayItem", Type =
typeof(DictionaryEntry))]
private DictionaryEntry[] _entry_extendedProperties
{
get
{
//make an array of dictionary entries to return
DictionaryEntry[]
ret =
new DictionaryEntry[ExtendedProperties.Count];
int i = 0;
DictionaryEntry
de;
//iterate through ExtendedProperties to load items into the array.
foreach (KeyValuePair<
string,
string> extendedProp
in ExtendedProperties)
{
de
=
new DictionaryEntry();
de.Key
= extendedProp.Key;
de.Value
= extendedProp.Value;
ret[i]
= de;
i++;
}
return ret;
}
set
{
if (ExtendedProperties ==
null)
{
ExtendedProperties
=
new Dictionary<
string,
string>();
}
ExtendedProperties.Clear();
for (
int i = 0; i <
value.Length; i++)
{
ExtendedProperties.Add((
string)
value[i].Key, (
string)
value[i].Value);
}
}
}
/// <summary>
/// Gets the error message with the <see cref="LogEntry"></see>
/// </summary>
/// <value>The error messages.</value>
public string ErrorMessages
{
get
{
return errorMessages; }
set
{ errorMessages =
value; }
}
/// <summary>
/// Gets the log entry.
/// </summary>
/// <param name="logEntrySerializable">The log entry serializable.</param>
/// <returns></returns>
public static LogEntry GetLogEntry(SerializableLogEntry logEntrySerializable)
{
LogEntry
logEntry =
new LogEntry();
logEntry.Message
= logEntrySerializable.Message;
logEntry.EventId
= logEntrySerializable.EventId;
logEntry.Title
= logEntrySerializable.Title;
if (!
string.IsNullOrEmpty(logEntrySerializable.Severity))
logEntry.Severity
= (TraceEventType)Enum.Parse(
typeof(TraceEventType), logEntrySerializable.Severity);
logEntry.Priority
= logEntrySerializable.Priority;
logEntry.TimeStamp
= logEntrySerializable.TimeStamp;
logEntry.MachineName
= logEntrySerializable.MachineName;
logEntry.AppDomainName
= logEntrySerializable.AppDomainName;
logEntry.ProcessId
= logEntrySerializable.ProcessId;
logEntry.ProcessName
= logEntrySerializable.ProcessName;
logEntry.ManagedThreadName
= logEntrySerializable.ManagedThreadName;
logEntry.Win32ThreadId
= logEntrySerializable.Win32ThreadId;
logEntry.AddErrorMessage(logEntrySerializable.ErrorMessages);
//TODO: Is there a better way to populate categories.
foreach (
string category
in logEntrySerializable.Categories)
logEntry.Categories.Add(category);
logEntry.ExtendedProperties
= logEntrySerializable.ExtendedProperties
as IDictionary<
string,
object>;
return logEntry;
}
}
}