📄 eventlogappender.cs
字号:
}
else if (!sourceAlreadyExists)
{
LogLog.Debug("EventLogAppender: Creating event source Source [" + m_applicationName + "] in log " + m_logName + "]");
}
string registeredLogName = null;
using(SecurityContext.Impersonate(this))
{
if (sourceAlreadyExists && currentLogName != m_logName)
{
//
// Re-register this to the current application if the user has changed
// the application / logfile association
//
EventLog.DeleteEventSource(m_applicationName, m_machineName);
CreateEventSource(m_applicationName, m_logName, m_machineName);
registeredLogName = EventLog.LogNameFromSourceName(m_applicationName, m_machineName);
}
else if (!sourceAlreadyExists)
{
CreateEventSource(m_applicationName, m_logName, m_machineName);
registeredLogName = EventLog.LogNameFromSourceName(m_applicationName, m_machineName);
}
}
m_levelMapping.ActivateOptions();
LogLog.Debug("EventLogAppender: Source [" + m_applicationName + "] is registered to log [" + registeredLogName + "]");
}
#endregion // Implementation of IOptionHandler
/// <summary>
/// Create an event log source
/// </summary>
/// <remarks>
/// Uses different API calls under NET_2_0
/// </remarks>
private static void CreateEventSource(string source, string logName, string machineName)
{
#if NET_2_0
EventSourceCreationData eventSourceCreationData = new EventSourceCreationData(source, logName);
eventSourceCreationData.MachineName = machineName;
EventLog.CreateEventSource(eventSourceCreationData);
#else
EventLog.CreateEventSource(source, logName, machineName);
#endif
}
#region Override implementation of AppenderSkeleton
/// <summary>
/// This method is called by the <see cref="AppenderSkeleton.DoAppend(LoggingEvent)"/>
/// method.
/// </summary>
/// <param name="loggingEvent">the event to log</param>
/// <remarks>
/// <para>Writes the event to the system event log using the
/// <see cref="ApplicationName"/>.</para>
///
/// <para>If the event has an <c>EventID</c> property (see <see cref="LoggingEvent.Properties"/>)
/// set then this integer will be used as the event log event id.</para>
///
/// <para>
/// There is a limit of 32K characters for an event log message
/// </para>
/// </remarks>
override protected void Append(LoggingEvent loggingEvent)
{
//
// Write the resulting string to the event log system
//
int eventID = 0;
// Look for the EventLogEventID property
object eventIDPropertyObj = loggingEvent.LookupProperty("EventID");
if (eventIDPropertyObj != null)
{
if (eventIDPropertyObj is int)
{
eventID = (int)eventIDPropertyObj;
}
else
{
string eventIDPropertyString = eventIDPropertyObj as string;
if (eventIDPropertyString != null && eventIDPropertyString.Length > 0)
{
// Read the string property into a number
int intVal;
if (SystemInfo.TryParse(eventIDPropertyString, out intVal))
{
eventID = intVal;
}
else
{
ErrorHandler.Error("Unable to parse event ID property [" + eventIDPropertyString + "].");
}
}
}
}
// Write to the event log
try
{
string eventTxt = RenderLoggingEvent(loggingEvent);
// There is a limit of 32K characters for an event log message
if (eventTxt.Length > 32000)
{
eventTxt = eventTxt.Substring(0, 32000);
}
EventLogEntryType entryType = GetEntryType(loggingEvent.Level);
using(SecurityContext.Impersonate(this))
{
EventLog.WriteEntry(m_applicationName, eventTxt, entryType, eventID);
}
}
catch(Exception ex)
{
ErrorHandler.Error("Unable to write to event log [" + m_logName + "] using source [" + m_applicationName + "]", ex);
}
}
/// <summary>
/// This appender requires a <see cref="Layout"/> to be set.
/// </summary>
/// <value><c>true</c></value>
/// <remarks>
/// <para>
/// This appender requires a <see cref="Layout"/> to be set.
/// </para>
/// </remarks>
override protected bool RequiresLayout
{
get { return true; }
}
#endregion // Override implementation of AppenderSkeleton
#region Protected Instance Methods
/// <summary>
/// Get the equivalent <see cref="EventLogEntryType"/> for a <see cref="Level"/> <paramref name="p"/>
/// </summary>
/// <param name="level">the Level to convert to an EventLogEntryType</param>
/// <returns>The equivalent <see cref="EventLogEntryType"/> for a <see cref="Level"/> <paramref name="p"/></returns>
/// <remarks>
/// Because there are fewer applicable <see cref="EventLogEntryType"/>
/// values to use in logging levels than there are in the
/// <see cref="Level"/> this is a one way mapping. There is
/// a loss of information during the conversion.
/// </remarks>
virtual protected EventLogEntryType GetEntryType(Level level)
{
// see if there is a specified lookup.
Level2EventLogEntryType entryType = m_levelMapping.Lookup(level) as Level2EventLogEntryType;
if (entryType != null)
{
return entryType.EventLogEntryType;
}
// Use default behavior
if (level >= Level.Error)
{
return EventLogEntryType.Error;
}
else if (level == Level.Warn)
{
return EventLogEntryType.Warning;
}
// Default setting
return EventLogEntryType.Information;
}
#endregion // Protected Instance Methods
#region Private Instance Fields
/// <summary>
/// The log name is the section in the event logs where the messages
/// are stored.
/// </summary>
private string m_logName;
/// <summary>
/// Name of the application to use when logging. This appears in the
/// application column of the event log named by <see cref="m_logName"/>.
/// </summary>
private string m_applicationName;
/// <summary>
/// The name of the machine which holds the event log. This is
/// currently only allowed to be '.' i.e. the current machine.
/// </summary>
private string m_machineName;
/// <summary>
/// Mapping from level object to EventLogEntryType
/// </summary>
private LevelMapping m_levelMapping = new LevelMapping();
/// <summary>
/// The security context to use for privileged calls
/// </summary>
private SecurityContext m_securityContext;
#endregion // Private Instance Fields
#region Level2EventLogEntryType LevelMapping Entry
/// <summary>
/// A class to act as a mapping between the level that a logging call is made at and
/// the color it should be displayed as.
/// </summary>
/// <remarks>
/// <para>
/// Defines the mapping between a level and its event log entry type.
/// </para>
/// </remarks>
public class Level2EventLogEntryType : LevelMappingEntry
{
private EventLogEntryType m_entryType;
/// <summary>
/// The <see cref="EventLogEntryType"/> for this entry
/// </summary>
/// <remarks>
/// <para>
/// Required property.
/// The <see cref="EventLogEntryType"/> for this entry
/// </para>
/// </remarks>
public EventLogEntryType EventLogEntryType
{
get { return m_entryType; }
set { m_entryType = value; }
}
}
#endregion // LevelColors LevelMapping Entry
}
}
#endif // !SSCLI
#endif // !NETCF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -