private void DeleteDefaultTraceLogFile() { const string listenerName = "xmlTextWriter"; const string fieldName = "initializeData"; TraceListener traceListener = Trace.Listeners[listenerName]; FieldInfo fieldInfo = traceListener?.GetType() .GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); var logFile = (string)fieldInfo?.GetValue(traceListener); if (logFile != null && File.Exists(logFile)) { File.Delete(logFile); } }
/// <summary> /// /// <example> /// <code> /// string info = GetAttributeValue(null, "rollingXmlWriter", "initializeData"); /// </code> /// </example> /// </summary> /// <param name="traceSourceName"></param> /// <param name="listenerName"></param> /// <param name="fieldName"></param> /// <returns></returns> public string GetAttributeValue(string traceSourceName, string listenerName, string fieldName) { if (string.IsNullOrEmpty(fieldName)) { throw new ArgumentNullException(nameof(fieldName)); } if (string.IsNullOrEmpty(listenerName)) { throw new ArgumentNullException(nameof(listenerName)); } TraceSource traceSource = GetTraceSource(traceSourceName); TraceListenerCollection traceListenerCollection = traceSource.Listeners; TraceListener traceListener = traceListenerCollection[listenerName]; FieldInfo fieldInfo = traceListener?.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); var fieldValue = (string)fieldInfo?.GetValue(traceListener); return(fieldValue); }
public void CanCreateInstanceFromConfigurationFile() { CustomTraceListenerData listenerData = new CustomTraceListenerData("listener", typeof(MockCustomTraceListener), initializationData); listenerData.SetAttributeValue(MockCustomTraceListener.AttributeKey, attributeValue); LoggingSettings loggingSettings = new LoggingSettings(); loggingSettings.TraceListeners.Add(listenerData); TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, "listener", CommonUtil.SaveSectionsAndGetConfigurationSource(loggingSettings), reflectionCache); Assert.IsNotNull(listener); Assert.AreEqual("listener", listener.Name); Assert.AreEqual(typeof(MockCustomTraceListener), listener.GetType()); Assert.AreEqual(initializationData, ((MockCustomTraceListener)listener).initData); Assert.IsNull(((MockCustomTraceListener)listener).Formatter); Assert.AreEqual(attributeValue, ((MockCustomTraceListener)listener).Attribute); }
public void CanCreateInstanceFromGivenConfiguration() { EmailTraceListenerData listenerData = new EmailTraceListenerData("listener", "[email protected]", "*****@*****.**", "EntLib-Logging:", "has occurred", "smtphost", 25, "formatter"); MockLogObjectsHelper helper = new MockLogObjectsHelper(); helper.loggingSettings.Formatters.Add(new TextFormatterData("formatter", "foobar template")); TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, listenerData, helper.configurationSource, reflectionCache); Assert.IsNotNull(listener); Assert.AreEqual("listener", listener.Name); Assert.AreEqual(listener.GetType(), typeof(EmailTraceListener)); Assert.IsNotNull(((EmailTraceListener)listener).Formatter); Assert.AreEqual(((EmailTraceListener)listener).Formatter.GetType(), typeof(TextFormatter)); Assert.AreEqual("foobar template", ((TextFormatter)((EmailTraceListener)listener).Formatter).Template); }
public void CanCreateInstanceFromConfigurationFile() { LoggingSettings loggingSettings = new LoggingSettings(); loggingSettings.Formatters.Add(new FormatterData(formatterName, typeof(BinaryLogFormatter))); loggingSettings.TraceListeners.Add(new MsmqTraceListenerData("listener", CommonUtil.MessageQueuePath, formatterName)); TraceListener listener = GetListener("listener", CommonUtil.SaveSectionsAndGetConfigurationSource(loggingSettings)); Assert.IsNotNull(listener); Assert.AreEqual(listener.GetType(), typeof(MsmqTraceListener)); MsmqTraceListener msmqTraceListener = listener as MsmqTraceListener; Assert.IsNotNull(msmqTraceListener.Formatter); Assert.AreEqual(msmqTraceListener.Formatter.GetType(), typeof(BinaryLogFormatter)); Assert.AreEqual(CommonUtil.MessageQueuePath, msmqTraceListener.QueuePath); }
public void CanCreatePoliciesForSysDiagsTraceListenerWithoutInitData() { SystemDiagnosticsTraceListenerData listenerData = new SystemDiagnosticsTraceListenerData("listener", typeof(MockCustomTraceListener), "", TraceOptions.Callstack); listenerData.SetAttributeValue(MockCustomTraceListener.AttributeKey, attributeValue); loggingSettings.TraceListeners.Add(listenerData); container.AddExtension(new LoggingBlockExtension()); TraceListener createdObject = container.Resolve <TraceListener>("listener"); Assert.IsNotNull(createdObject); Assert.AreEqual("listener", createdObject.Name); Assert.AreEqual(TraceOptions.Callstack, createdObject.TraceOutputOptions); Assert.AreEqual(typeof(MockCustomTraceListener), createdObject.GetType()); Assert.AreEqual(null, ((MockCustomTraceListener)createdObject).initData); // configured with "", but set to null Assert.AreEqual(attributeValue, ((MockCustomTraceListener)createdObject).Attribute); }
public void CanCreateInstanceFromConfigurationFile() { SystemDiagnosticsTraceListenerData listenerData = new SystemDiagnosticsTraceListenerData("listener", typeof(DelimitedListTraceListener), "log.txt"); listenerData.SetAttributeValue("delimiter", "||"); LoggingSettings loggingSettings = new LoggingSettings(); loggingSettings.TraceListeners.Add(listenerData); TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, "listener", CommonUtil.SaveSectionsAndGetConfigurationSource(loggingSettings), reflectionCache); Assert.IsNotNull(listener); Assert.AreEqual("listener", listener.Name); Assert.AreEqual(typeof(DelimitedListTraceListener), listener.GetType()); Assert.AreEqual("||", ((DelimitedListTraceListener)listener).Delimiter); }
public void CanCreateInstanceFromConfigurationFile() { LoggingSettings loggingSettings = new LoggingSettings(); loggingSettings.Formatters.Add(new TextFormatterData("formatter", "foobar template")); loggingSettings.TraceListeners.Add( new FormattedDatabaseTraceListenerData("listener", "WriteLog", "AddCategory", "LoggingDb", "formatter")); TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, "listener", CommonUtil.SaveSectionsAndGetConfigurationSource(loggingSettings), reflectionCache); Assert.IsNotNull(listener); Assert.AreEqual(listener.GetType(), typeof(FormattedDatabaseTraceListener)); Assert.IsNotNull(((FormattedDatabaseTraceListener)listener).Formatter); Assert.AreEqual(((FormattedDatabaseTraceListener)listener).Formatter.GetType(), typeof(TextFormatter)); Assert.AreEqual("foobar template", ((TextFormatter)((FormattedDatabaseTraceListener)listener).Formatter).Template); }
public void CanCreateInstanceFromGivenConfiguration() { TraceListenerData listenerData = new MsmqTraceListenerData("listener", CommonUtil.MessageQueuePath, formatterName); MockLogObjectsHelper helper = new MockLogObjectsHelper(); helper.loggingSettings.Formatters.Add(new BinaryLogFormatterData(formatterName)); TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, listenerData, helper.configurationSource, reflectionCache); Assert.IsNotNull(listener); Assert.AreEqual(listener.GetType(), typeof(MsmqTraceListener)); MsmqTraceListener msmqTraceListener = listener as MsmqTraceListener; Assert.AreEqual("listener", listener.Name); Assert.IsNotNull(msmqTraceListener.Formatter); Assert.AreEqual(msmqTraceListener.Formatter.GetType(), typeof(BinaryLogFormatter)); Assert.AreEqual(CommonUtil.MessageQueuePath, msmqTraceListener.QueuePath); }
public void CanCreateInstanceFromConfigurationFile() { LoggingSettings loggingSettings = new LoggingSettings(); loggingSettings.Formatters.Add(new TextFormatterData("formatter", "foobar template")); loggingSettings.TraceListeners.Add(new FormattedEventLogTraceListenerData("listener", "unknown source", "log", "machine", "formatter")); TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, "listener", CommonUtil.SaveSectionsAndGetConfigurationSource(loggingSettings), reflectionCache); Assert.IsNotNull(listener); Assert.AreEqual(listener.GetType(), typeof(FormattedEventLogTraceListener)); FormattedEventLogTraceListener castedListener = (FormattedEventLogTraceListener)listener; Assert.IsNotNull(castedListener.Formatter); Assert.AreEqual("unknown source", ((EventLogTraceListener)castedListener.SlaveListener).EventLog.Source); Assert.AreEqual("log", ((EventLogTraceListener)castedListener.SlaveListener).EventLog.Log); Assert.AreEqual("machine", ((EventLogTraceListener)castedListener.SlaveListener).EventLog.MachineName); Assert.AreEqual(castedListener.Formatter.GetType(), typeof(TextFormatter)); Assert.AreEqual("foobar template", ((TextFormatter)castedListener.Formatter).Template); }
public void CanCreateInstanceFromGivenName() { TraceListenerData listenerData = new MsmqTraceListenerData("listener", CommonUtil.MessageQueuePath, formatterName); MockLogObjectsHelper helper = new MockLogObjectsHelper(); helper.loggingSettings.TraceListeners.Add(listenerData); helper.loggingSettings.Formatters.Add(new BinaryLogFormatterData(formatterName)); TraceListener listener = GetListener("listener\u200cimplementation", helper.configurationSource); Assert.IsNotNull(listener); Assert.AreEqual(listener.GetType(), typeof(MsmqTraceListener)); MsmqTraceListener msmqTraceListener = listener as MsmqTraceListener; Assert.AreEqual("listener\u200cimplementation", listener.Name); Assert.IsNotNull(msmqTraceListener.Formatter); Assert.AreEqual(msmqTraceListener.Formatter.GetType(), typeof(BinaryLogFormatter)); Assert.AreEqual(CommonUtil.MessageQueuePath, msmqTraceListener.QueuePath); }
static IWmiInstance[] CreateListenersInfo(TraceSource traceSource, IWmiInstance instance) { Fx.Assert(null != traceSource, ""); Fx.Assert(null != instance, ""); IWmiInstance[] traceListeners = new IWmiInstance[traceSource.Listeners.Count]; for (int i = 0; i < traceSource.Listeners.Count; i++) { TraceListener traceListener = traceSource.Listeners[i]; IWmiInstance traceListenerWmiInstance = instance.NewInstance(AdministrationStrings.TraceListener); traceListenerWmiInstance.SetProperty(AdministrationStrings.Name, traceListener.Name); List <IWmiInstance> traceListenerArguments = new List <IWmiInstance>(1); Type type = traceListener.GetType(); string initializeData = (string)type.InvokeMember(AdministrationStrings.InitializeData, BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, traceListener, null, CultureInfo.InvariantCulture); string[] supportedAttributes = (string[])type.InvokeMember(AdministrationStrings.GetSupportedAttributes, BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, traceListener, null, CultureInfo.InvariantCulture); IWmiInstance argumentWmiInstance = instance.NewInstance(AdministrationStrings.TraceListenerArgument); argumentWmiInstance.SetProperty(AdministrationStrings.Name, AdministrationStrings.InitializeData); argumentWmiInstance.SetProperty(AdministrationStrings.Value, initializeData); traceListenerArguments.Add(argumentWmiInstance); if (null != supportedAttributes) { foreach (string attribute in supportedAttributes) { argumentWmiInstance = instance.NewInstance(AdministrationStrings.TraceListenerArgument); argumentWmiInstance.SetProperty(AdministrationStrings.Name, attribute); argumentWmiInstance.SetProperty(AdministrationStrings.Value, traceListener.Attributes[attribute]); traceListenerArguments.Add(argumentWmiInstance); } } traceListenerWmiInstance.SetProperty(AdministrationStrings.TraceListenerArguments, traceListenerArguments.ToArray()); traceListeners[i] = traceListenerWmiInstance; } return(traceListeners); }
public void CanCreateInstanceFromGivenConfiguration() { FormattedEventLogTraceListenerData listenerData = new FormattedEventLogTraceListenerData("listener", "unknown source", "formatter"); MockLogObjectsHelper helper = new MockLogObjectsHelper(); helper.loggingSettings.Formatters.Add(new TextFormatterData("formatter", "foobar template")); TraceListener listener = TraceListenerCustomFactory.Instance.Create(context, listenerData, helper.configurationSource, reflectionCache); Assert.IsNotNull(listener); Assert.AreEqual("listener", listener.Name); Assert.AreEqual(listener.GetType(), typeof(FormattedEventLogTraceListener)); FormattedEventLogTraceListener castedListener = (FormattedEventLogTraceListener)listener; Assert.IsNotNull(castedListener.Formatter); Assert.AreEqual("unknown source", ((EventLogTraceListener)castedListener.SlaveListener).EventLog.Source); Assert.AreEqual(FormattedEventLogTraceListener.DefaultLogName, ((EventLogTraceListener)castedListener.SlaveListener).EventLog.Log); Assert.AreEqual(FormattedEventLogTraceListener.DefaultMachineName, ((EventLogTraceListener)castedListener.SlaveListener).EventLog.MachineName); Assert.AreEqual(castedListener.Formatter.GetType(), typeof(TextFormatter)); Assert.AreEqual("foobar template", ((TextFormatter)castedListener.Formatter).Template); }
public void CanCreateInstanceFromGivenName() { FormattedEventLogTraceListenerData listenerData = new FormattedEventLogTraceListenerData("listener", "unknown source", "formatter"); MockLogObjectsHelper helper = new MockLogObjectsHelper(); helper.loggingSettings.Formatters.Add(new TextFormatterData("formatter", "foobar template")); helper.loggingSettings.TraceListeners.Add(listenerData); TraceListener listener = GetListener("listener\u200cimplementation", helper.configurationSource); Assert.IsNotNull(listener); Assert.AreEqual("listener\u200cimplementation", listener.Name); Assert.AreEqual(listener.GetType(), typeof(FormattedEventLogTraceListener)); FormattedEventLogTraceListener castedListener = (FormattedEventLogTraceListener)listener; Assert.IsNotNull(castedListener.Formatter); Assert.AreEqual("unknown source", ((EventLogTraceListener)castedListener.InnerListener).EventLog.Source); Assert.AreEqual(FormattedEventLogTraceListener.DefaultLogName, ((EventLogTraceListener)castedListener.InnerListener).EventLog.Log); Assert.AreEqual(FormattedEventLogTraceListener.DefaultMachineName, ((EventLogTraceListener)castedListener.InnerListener).EventLog.MachineName); Assert.AreEqual(castedListener.Formatter.GetType(), typeof(TextFormatter)); Assert.AreEqual("foobar template", ((TextFormatter)castedListener.Formatter).Template); }
/// <summary> /// Replaces the listener of the same type as given in both the Options.Listeners and in the System.Diagnostics.Trace.Listeners. /// If there is no listener to replace the listener will be added instead. /// </summary> /// <param name="newListener">The new listener.</param> /// <returns>A reference to this builder.</returns> public TraceSourceLogFactoryBuilder ReplaceListener(TraceListener newListener) { var currentListeners = new List <TraceListener>(Options.Listeners); foreach (var listener in currentListeners) { if (newListener.GetType().Equals(listener.GetType())) { try { Options.Listeners.Remove(listener); } catch { } try { System.Diagnostics.Trace.Listeners.Remove(listener); } catch { } } } AddTraceListener(newListener); System.Diagnostics.Trace.Listeners.Add(newListener); return(this); }
/// <summary> /// Returns true if this entry in the config describes the supplied TraceListener object /// </summary> public bool Describes(TraceListener listener) { return((Name == listener.Name) && listener.GetType().FullName.StartsWith(TypeName)); }
public IRunTrace RunTrace(TraceListener traceListener) { _actions.Add(new Tuple <object, Action <object> >(traceListener, x => { Ex.Trace.Write(new ProgramStarterMessage(string.Format(Ex.Rs <StartTraceSetting> .Description, traceListener.GetType().FullName))); System.Diagnostics.Trace.Listeners.Add((TraceListener)x); })); return(this); }
public static IRunTrace RunTrace(TraceListener traceListener) { return(new ProgramStarter(traceListener, x => { System.Diagnostics.Trace.Listeners.Add((TraceListener)x); Ex.Trace.Write(new ProgramStarterMessage("Loading resources")); AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException; AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; var lang = ConfigurationManager.AppSettings["Culture"]; if (string.IsNullOrWhiteSpace(lang)) { lang = "en-US"; } Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang); Ex.Trace.Write(new ProgramStarterMessage(string.Format(Ex.Rs <StartAppLangSetting> .Description, lang))); Ex.Trace.Write(Ex.Rs <StartNLogBaseSetting> .Description); Ex.Trace.Write(new ProgramStarterMessage(string.Format(Ex.Rs <StartTraceSetting> .Description, traceListener.GetType().FullName))); })); }
private static bool IsDevFabricTraceListener(TraceListener listener) { return(String.Compare(listener.GetType().Name, Resources.DevelopmentFabricTraceListenerName, true) == 0); }