Example #1
0
        static public void InitializeLogging(string fileName)
        {
            LoggingSettings loggingSettings = (LoggingSettings)config.GetSection("loggingConfiguration");

            Debug.Assert(loggingSettings != null);
            TraceListenerData traceListenerData = loggingSettings.TraceListeners.Get("Flat File Trace Listener");

            Debug.Assert(traceListenerData != null);
            FlatFileTraceListenerData file = traceListenerData as FlatFileTraceListenerData;

            Debug.Assert(file != null);

            if (fileName == "")
            {
                File.Copy(file.FileName, LoggingDirectory + Path.GetFileName(file.FileName));
                file.FileName = LoggingDirectory + Path.GetFileName(file.FileName);
            }
            else
            {
                file.FileName    = LoggingDirectory + "\\" + fileName + ".log";
                loggingDirectory = Path.GetDirectoryName(file.FileName);
            }

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("loggingConfiguration");
        }
    public void SetLogFilePath(string filePath)
    {
        ConfigurationFileMap objConfigPath = new ConfigurationFileMap();
        // App config file path.
        string appPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

        objConfigPath.MachineConfigFilename = appPath;
        Configuration             entLibConfig                 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
        LoggingSettings           loggingSettings              = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);
        TraceListenerData         traceListenerData            = loggingSettings.TraceListeners.Get("Flat File Trace Listener");
        FlatFileTraceListenerData objFlatFileTraceListenerData = traceListenerData as FlatFileTraceListenerData;

        objFlatFileTraceListenerData.FileName = filePath;

        IUnityContainer container = new UnityContainer();

        container.AddNewExtension <EnterpriseLibraryCoreExtension>();
        // Configurator will read Enterprise Library configuration
        // and set up the container
        UnityContainerConfigurator configurator = new UnityContainerConfigurator(container);
        var loggingXmlConfigSource = new SerializableConfigurationSource();

        loggingXmlConfigSource.Add(LoggingSettings.SectionName, loggingSettings);
        // Configure the container with our own custom logging
        EnterpriseLibraryContainer.ConfigureContainer(configurator, loggingXmlConfigSource);
        // Wrap in ServiceLocator
        IServiceLocator locator = new UnityServiceLocator(container);

        // And set Enterprise Library to use it
        EnterpriseLibraryContainer.Current = locator;
    }
Example #3
0
        public void CanDeserializeSerializedConfiguration()
        {
            string name      = "name";
            string filename  = "filename";
            string header    = "header";
            string footer    = "footer";
            string formatter = "formatter";

            TraceListenerData data = new FlatFileTraceListenerData(name, filename, header, footer,
                                                                   formatter, TraceOptions.Callstack);

            LoggingSettings settings = new LoggingSettings();

            settings.TraceListeners.Add(data);

            IDictionary <string, ConfigurationSection> sections = new Dictionary <string, ConfigurationSection>();

            sections[LoggingSettings.SectionName] = settings;
            IConfigurationSource configurationSource
                = ConfigurationTestHelper.SaveSectionsInFileAndReturnConfigurationSource(sections);

            LoggingSettings roSettigs = (LoggingSettings)configurationSource.GetSection(LoggingSettings.SectionName);

            Assert.AreEqual(1, roSettigs.TraceListeners.Count);
            Assert.IsNotNull(roSettigs.TraceListeners.Get(name));
            Assert.AreEqual(TraceOptions.Callstack, roSettigs.TraceListeners.Get(name).TraceOutputOptions);
            Assert.AreSame(typeof(FlatFileTraceListenerData), roSettigs.TraceListeners.Get(name).GetType());
            Assert.AreSame(typeof(FlatFileTraceListenerData), roSettigs.TraceListeners.Get(name).ListenerDataType);
            Assert.AreSame(typeof(FlatFileTraceListener), roSettigs.TraceListeners.Get(name).Type);
            Assert.AreEqual(filename, ((FlatFileTraceListenerData)roSettigs.TraceListeners.Get(name)).FileName);
            Assert.AreEqual(footer, ((FlatFileTraceListenerData)roSettigs.TraceListeners.Get(name)).Footer);
            Assert.AreEqual(formatter, ((FlatFileTraceListenerData)roSettigs.TraceListeners.Get(name)).Formatter);
            Assert.AreEqual(header, ((FlatFileTraceListenerData)roSettigs.TraceListeners.Get(name)).Header);
        }
        public void SavesChangesToConfigurationObject()
        {
            FlatFileTraceListenerData sourceElement = new FlatFileTraceListenerData();

            sourceElement.FileName           = "file name";
            sourceElement.Footer             = "footer";
            sourceElement.Formatter          = "formatter";
            sourceElement.Header             = "header";
            sourceElement.Filter             = SourceLevels.Information;
            sourceElement.TraceOutputOptions = TraceOptions.ProcessId;
            List <ConfigurationSetting> settings = new List <ConfigurationSetting>(1);

            FlatFileTraceListenerDataWmiMapper.GenerateWmiObjects(sourceElement, settings);
            Assert.AreEqual(1, settings.Count);
            FlatFileTraceListenerSetting setting = settings[0] as FlatFileTraceListenerSetting;

            Assert.IsNotNull(setting);
            setting.FileName           = "updated file name";
            setting.Footer             = "updated footer";
            setting.Formatter          = "updated formatter";
            setting.Header             = "updated header";
            setting.Filter             = SourceLevels.All.ToString();
            setting.TraceOutputOptions = TraceOptions.ThreadId.ToString();
            setting.Commit();
            Assert.AreEqual("updated file name", sourceElement.FileName);
            Assert.AreEqual("updated footer", sourceElement.Footer);
            Assert.AreEqual("updated formatter", sourceElement.Formatter);
            Assert.AreEqual("updated header", sourceElement.Header);
            Assert.AreEqual(SourceLevels.All, sourceElement.Filter);
            Assert.AreEqual(TraceOptions.ThreadId, sourceElement.TraceOutputOptions);
        }
Example #5
0
 public void SetUp()
 {
     provider            = new ConfigurationElementManageabilityProviderWrapper(new FlatFileTraceListenerDataManageabilityProvider());
     machineKey          = new MockRegistryKey(true);
     userKey             = new MockRegistryKey(true);
     wmiSettings         = new List <ConfigurationSetting>();
     configurationObject = new FlatFileTraceListenerData();
 }
Example #6
0
 public void SetUp()
 {
     provider            = new FlatFileTraceListenerDataManageabilityProvider();
     machineKey          = new MockRegistryKey(true);
     machineOptionsKey   = new MockRegistryKey(false);
     userKey             = new MockRegistryKey(true);
     userOptionsKey      = new MockRegistryKey(false);
     configurationObject = new FlatFileTraceListenerData();
 }
            public SendToFlatFileTraceListenerBuilder(ILoggingConfigurationSendTo context, string listenerName)
                : base(context)
            {
                flatFileTracelistenerData = new FlatFileTraceListenerData
                {
                    Name = listenerName
                };

                base.AddTraceListenerToSettingsAndCategory(flatFileTracelistenerData);
            }
        /// <summary>
        /// Initialize a new instance of the <see cref="FlatFileTraceListenerNode"/> class with a <see cref="FlatFileTraceListenerData"/> instance.
        /// </summary>
        /// <param name="traceListenerData">A <see cref="FlatFileTraceListenerData"/> instance.</param>
        public FlatFileTraceListenerNode(FlatFileTraceListenerData traceListenerData)
        {
            if (null == traceListenerData) throw new ArgumentNullException("traceListenerData");

            Rename(traceListenerData.Name);
            TraceOutputOptions = traceListenerData.TraceOutputOptions;
            this.formatterName = traceListenerData.Formatter;
            this.fileName = traceListenerData.FileName;
            this.header = traceListenerData.Header;
            this.footer = traceListenerData.Footer;
        }
Example #9
0
        public void ListenerDataIsCreatedCorrectly()
        {
            FlatFileTraceListenerData listenerData = new FlatFileTraceListenerData("listener", "log.txt", "---header---", "+++footer+++", "formatter");

            Assert.AreSame(typeof(FlatFileTraceListener), listenerData.Type);
            Assert.AreSame(typeof(FlatFileTraceListenerData), listenerData.ListenerDataType);
            Assert.AreEqual("listener", listenerData.Name);
            Assert.AreEqual("log.txt", listenerData.FileName);
            Assert.AreEqual("---header---", listenerData.Header);
            Assert.AreEqual("+++footer+++", listenerData.Footer);
            Assert.AreEqual("formatter", listenerData.Formatter);
        }
 public FlatFileTraceListenerNode(FlatFileTraceListenerData traceListenerData)
 {
     if (null == traceListenerData)
     {
         throw new ArgumentNullException("traceListenerData");
     }
     Rename(traceListenerData.Name);
     TraceOutputOptions = traceListenerData.TraceOutputOptions;
     this.formatterName = traceListenerData.Formatter;
     this.fileName      = traceListenerData.FileName;
     this.header        = traceListenerData.Header;
     this.footer        = traceListenerData.Footer;
 }
Example #11
0
 public static void GenerateWmiObjects(FlatFileTraceListenerData configurationObject,
                                       ICollection <ConfigurationSetting> wmiSettings)
 {
     wmiSettings.Add(
         new FlatFileTraceListenerSetting(configurationObject,
                                          configurationObject.Name,
                                          configurationObject.FileName,
                                          configurationObject.Header,
                                          configurationObject.Footer,
                                          configurationObject.Formatter,
                                          configurationObject.TraceOutputOptions.ToString(),
                                          configurationObject.Filter.ToString()));
 }
 public FlatFileTraceListenerSetting(FlatFileTraceListenerData sourceElement,
                                     string name,
                                     string fileName,
                                     string header,
                                     string footer,
                                     string formatter,
                                     string traceOutputOptions,
                                     string filter)
     : base(sourceElement, name, traceOutputOptions, filter)
 {
     this.fileName  = fileName;
     this.header    = header;
     this.footer    = footer;
     this.formatter = formatter;
 }
        public void CanCreateInstanceFromGivenConfiguration()
        {
            FlatFileTraceListenerData listenerData = new FlatFileTraceListenerData("listener", "log.txt", "---header---", "+++footer+++", "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(FlatFileTraceListener));
            Assert.IsNotNull(((FlatFileTraceListener)listener).Formatter);
            Assert.AreEqual(((FlatFileTraceListener)listener).Formatter.GetType(), typeof(TextFormatter));
            Assert.AreEqual("foobar template", ((TextFormatter)((FlatFileTraceListener)listener).Formatter).Template);
        }
Example #14
0
        public void TraceListenerIsSingletonInContainer()
        {
            FormatterData data = new TextFormatterData("formattername", "template");

            loggingSettings.Formatters.Add(data);
            TraceListenerData traceListenerData = new FlatFileTraceListenerData("name", "filename.log", "formattername");

            loggingSettings.TraceListeners.Add(traceListenerData);

            InitializeContainer();

            TraceListener createdObject1 = container.Resolve <TraceListener>("name");
            TraceListener createdObject2 = container.Resolve <TraceListener>("name");

            Assert.AreSame(createdObject1, createdObject2);
        }
        public void TraceListenerIsSingletonInContainer()
        {
            FormatterData data = new TextFormatterData("formattername", "template");

            loggingSettings.Formatters.Add(data);
            TraceListenerData traceListenerData = new FlatFileTraceListenerData("name", "filename.log", "formattername");

            loggingSettings.TraceListeners.Add(traceListenerData);

            container.AddExtension(new LoggingBlockExtension());

            FlatFileTraceListener createdObject1 = (FlatFileTraceListener)container.Resolve <TraceListener>("name");
            FlatFileTraceListener createdObject2 = (FlatFileTraceListener)container.Resolve <TraceListener>("name");

            Assert.AreSame(createdObject1, createdObject2);
        }
Example #16
0
        internal static bool SaveChanges(FlatFileTraceListenerSetting setting, ConfigurationElement sourceElement)
        {
            FlatFileTraceListenerData element = (FlatFileTraceListenerData)sourceElement;

            element.FileName  = setting.FileName;
            element.Footer    = setting.Footer;
            element.Formatter = setting.Formatter;
            element.Header    = setting.Header;
            SourceLevels filter;

            if (ParseHelper.TryParseEnum(setting.Filter, out filter))
            {
                element.Filter = filter;
            }
            element.TraceOutputOptions = ParseHelper.ParseEnum <TraceOptions>(setting.TraceOutputOptions, false);
            return(true);
        }
Example #17
0
        public void CanCreateInstanceFromGiveName()
        {
            FlatFileTraceListenerData listenerData = new FlatFileTraceListenerData("listener", "log.txt", "---header---", "+++footer+++", "formatter");

            MockLogObjectsHelper helper = new MockLogObjectsHelper();

            helper.loggingSettings.TraceListeners.Add(listenerData);
            helper.loggingSettings.Formatters.Add(new TextFormatterData("formatter", "some template"));

            TraceListener listener = GetListener("listener", helper.configurationSource);

            Assert.IsNotNull(listener);
            Assert.AreEqual("listener", listener.Name);
            Assert.AreEqual(listener.GetType(), typeof(FlatFileTraceListener));
            Assert.IsNotNull(((FlatFileTraceListener)listener).Formatter);
            Assert.AreEqual(((FlatFileTraceListener)listener).Formatter.GetType(), typeof(TextFormatter));
            Assert.AreEqual("some template", ((TextFormatter)((FlatFileTraceListener)listener).Formatter).Template);
        }
Example #18
0
        public void FlatFileTraceListenerNodeDataTest()
        {
            string name     = "some name";
            string fileName = "some filename";
            string header   = "some header";
            string footer   = "some footer";
            FlatFileTraceListenerData flatFileTraceListenerData = new FlatFileTraceListenerData();

            flatFileTraceListenerData.Name     = name;
            flatFileTraceListenerData.FileName = fileName;
            flatFileTraceListenerData.Header   = header;
            flatFileTraceListenerData.Footer   = footer;
            FlatFileTraceListenerNode flatFileTraceListenerNode = new FlatFileTraceListenerNode(flatFileTraceListenerData);

            ApplicationNode.AddNode(flatFileTraceListenerNode);
            Assert.AreEqual(name, flatFileTraceListenerNode.Name);
            Assert.AreEqual(fileName, flatFileTraceListenerNode.Filename);
            Assert.AreEqual(header, flatFileTraceListenerNode.Header);
            Assert.AreEqual(footer, flatFileTraceListenerNode.Footer);
        }
        public void FlatFileTraceListenerNodeDataTest()
        {
            string name = "some name";
            string fileName = "some filename";
            string header = "some header";
            string footer = "some footer";

            FlatFileTraceListenerData flatFileTraceListenerData = new FlatFileTraceListenerData();
            flatFileTraceListenerData.Name = name;
            flatFileTraceListenerData.FileName = fileName;
            flatFileTraceListenerData.Header = header;
            flatFileTraceListenerData.Footer = footer;

            FlatFileTraceListenerNode flatFileTraceListenerNode = new FlatFileTraceListenerNode(flatFileTraceListenerData);
            ApplicationNode.AddNode(flatFileTraceListenerNode);

            Assert.AreEqual(name, flatFileTraceListenerNode.Name);
            Assert.AreEqual(fileName, flatFileTraceListenerNode.Filename);
            Assert.AreEqual(header, flatFileTraceListenerNode.Header);
            Assert.AreEqual(footer, flatFileTraceListenerNode.Footer);
        }
        public void CanCreateTraceListenerWithReferenceToFormatter()
        {
            FormatterData data = new TextFormatterData("formattername", "template");

            loggingSettings.Formatters.Add(data);
            TraceListenerData traceListenerData = new FlatFileTraceListenerData("name", "filename.log", "formattername");

            traceListenerData.Filter             = SourceLevels.Critical;
            traceListenerData.TraceOutputOptions = TraceOptions.ProcessId;
            loggingSettings.TraceListeners.Add(traceListenerData);

            container.AddExtension(new LoggingBlockExtension());

            FlatFileTraceListener createdObject = (FlatFileTraceListener)container.Resolve <TraceListener>("name");

            Assert.IsNotNull(createdObject);
            Assert.AreEqual("name", createdObject.Name);
            Assert.AreEqual(SourceLevels.Critical, ((EventTypeFilter)createdObject.Filter).EventType);
            Assert.AreEqual(TraceOptions.ProcessId, createdObject.TraceOutputOptions);
            Assert.IsNotNull(createdObject.Formatter);
            Assert.AreEqual("template", ((TextFormatter)createdObject.Formatter).Template);
        }
 public void WhenCreatingInstanceUsingDefaultContructor_ThenListenerDataTypeIsSet()
 {
     var listener = new FlatFileTraceListenerData();
     Assert.AreEqual(typeof(FlatFileTraceListenerData), listener.ListenerDataType);
 }
        public void WhenCreatingInstanceUsingDefaultContructor_ThenListenerDataTypeIsSet()
        {
            var listener = new FlatFileTraceListenerData();

            Assert.AreEqual(typeof(FlatFileTraceListenerData), listener.ListenerDataType);
        }