#region Apache License // // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to you under the Apache License, Version 2.0 // (the "License"); you may not use this file except in compliance with // the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #endregion using System; using log4net.Core; using System.Management.Instrumentation; namespace log4net.Appender { public class WmiLayout { /// /// Formats a for instrumentation /// /// the containing the data /// an instrumentation event that can be fired /// /// /// If the of the /// is an then /// that instance is returned. If the instance also implements the /// interface then the /// method will be called on the instance with the /// parameter. /// /// /// If the of the /// is not an /// then the method will be called /// to create an appropriate instrumentation event object. /// /// public virtual IEvent Format(LoggingEvent loggingEvent) { // See if the caller gave us an Instrumentation Event IEvent instrumentationEvent = loggingEvent.MessageObject as IEvent; if (instrumentationEvent != null) { // See if the caller gave us a Bound Instrumentation Event IWmiBoundEvent boundEvent = instrumentationEvent as IWmiBoundEvent; if (boundEvent != null) { // Attach the logging event to the bound instrumentation event boundEvent.Bind(loggingEvent); } return instrumentationEvent; } // We must create our own IEvent return CreateEvent(loggingEvent); } /// /// Create the instance that should be fired /// /// the containing the data /// an instrumentation event that can be fired /// /// /// The default implementation of this method creates a /// instance using the data from the . /// /// /// Subclasses should override this method to return their own custom /// instrumentation event object. /// /// protected virtual IEvent CreateEvent(LoggingEvent loggingEvent) { WmiLoggingEvent wmiEvent = new WmiLoggingEvent(); wmiEvent.TimeStamp = loggingEvent.TimeStamp; wmiEvent.LoggerName = loggingEvent.LoggerName; wmiEvent.Level = loggingEvent.Level.DisplayName; wmiEvent.Message = loggingEvent.RenderedMessage; wmiEvent.ThreadName = loggingEvent.ThreadName; wmiEvent.ExceptionString = loggingEvent.GetExceptionString(); wmiEvent.Domain = loggingEvent.Domain; return wmiEvent; } } }