Example #1
0
        public static void DisplayProperties(TraceSource ts)
        {
            Console.WriteLine("TraceSource name = " + ts.Name);
            Console.WriteLine("TraceSource switch level = " + ts.Switch.Level);
            Console.WriteLine("TraceSource switch = " + ts.Switch.DisplayName);
            SwitchAttribute[] switches = SwitchAttribute.GetAll(Assembly.GetExecutingAssembly());
            for (int i = 0; i < switches.Length; i++)
            {
                Console.WriteLine("Switch name = " + switches[i].SwitchName);
                Console.WriteLine("Switch Type = " + switches[i].SwitchType);
            }

            // Get the custom attributes for the TraceSource.
            Console.WriteLine("Number of custom trace source attributes = "
                              + ts.Attributes.Count);
            foreach (DictionaryEntry de in ts.Attributes)
            {
                Console.WriteLine("Custom trace source attribute = "
                                  + de.Key + "  " + de.Value);
            }
            // Get the custom attributes for the trace source switch.
            foreach (DictionaryEntry de in ts.Switch.Attributes)
            {
                Console.WriteLine("Custom switch attribute = "
                                  + de.Key + "  " + de.Value);
            }
            Console.WriteLine("Number of listeners = " + ts.Listeners.Count);
            foreach (TraceListener traceListener in ts.Listeners)
            {
                Console.Write("TraceListener: " + traceListener.Name + "\t");
                // The following output can be used to update the configuration file.
                Console.WriteLine("AssemblyQualifiedName = " +
                                  (traceListener.GetType().AssemblyQualifiedName));
            }
        }
Example #2
0
 private static void SetSwitch(ExpandoObject field, SwitchAttribute switchAttribute)
 {
     field.TryAdd("type", ReactFormItemType.Switch);
     field.TryAdd("valuePropName", "checked");
     field.TryAdd("switch", new
     {
         switchAttribute.CheckedChildren,
         switchAttribute.UnCheckedChildren,
         switchAttribute.Disabled,
     });
 }
Example #3
0
        public void CreateCommandLineOptionsCollection_AddSingleItem_RecallByShortName_LowerCase()
        {
            CommandLineOptionsCollection collection = new CommandLineOptionsCollection();

            collection.Add(new SwitchAttribute("EnableLogging")
            {
                ShortHandName = "LOG"
            });
            SwitchAttribute attribute = (SwitchAttribute)collection["log"];

            Assert.IsNotNull(attribute, "Should have recalled the attribute.");
        }
        /// <summary>
        /// Create native marshaller.
        /// </summary>
        /// <param name="type">Type of struct.</param>
        /// <param name="switchValue">Switch attribute value if presents.</param>
        /// <param name="sizeValue">Size attribute value if presents.</param>
        /// <param name="lengthValue">Length attribute value if presents.</param>
        /// <param name="marshalDesccriptor">marshal descriptor</param>
        /// <returns>created marshaller</returns>
        private static Marshaler CreateNativeMarshaller(
            Type type,
            object switchValue,
            object sizeValue,
            object lengthValue,
            bool force32Bit,
            int align,
            out MarshalingDescriptor marshalDesccriptor
            )
        {
            MarshalingConfiguration mc = NativeMarshalingConfiguration.Configuration;

            if (align != -1)
            {
                mc.Alignment = align;
            }
            if (force32Bit)
            {
                mc.IntPtrSize = 4;
            }
            Marshaler marshaller = new Marshaler(mc);

            SwitchAttribute switchAttr = null;
            SizeAttribute   sizeAttr   = null;
            LengthAttribute lengthAttr = null;
            string          nameSuffix = DateTime.UtcNow.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture);

            if (switchValue != null)
            {
                string switchSymbolName = "switch_" + nameSuffix;
                marshaller.DefineSymbol(switchSymbolName, switchValue);
                switchAttr = new SwitchAttribute(switchSymbolName);
            }
            if (sizeValue != null)
            {
                string sizeSymbolName = "size_" + nameSuffix;
                marshaller.DefineSymbol(sizeSymbolName, sizeValue);
                sizeAttr = new SizeAttribute(sizeSymbolName);
            }
            if (lengthValue != null)
            {
                string lengthSymbolName = "length_" + nameSuffix;
                marshaller.DefineSymbol(lengthSymbolName, lengthValue);
                lengthAttr = new LengthAttribute(lengthSymbolName);
            }
            TypeCustomAttributeProvider attrProvider
                = new TypeCustomAttributeProvider(switchAttr, sizeAttr, lengthAttr);

            marshalDesccriptor = new MarshalingDescriptor(type, attrProvider);

            return(marshaller);
        }
Example #5
0
 private void GetAvailableSwitchProperties()
 {
     _availableSwitchProps = new List <SwitchParameterEntity>();
     foreach (PropertyInfo _pi in _paramsObject.GetType().GetProperties())
     {
         SwitchAttribute _attr = _pi.GetCustomAttribute <SwitchAttribute>();
         if (_attr != null)
         {
             _availableSwitchProps.Add(new SwitchParameterEntity {
                 SwitchProperty = _pi, SwitchAttribute = _attr
             });
         }
     }
 }
Example #6
0
        public ConfigurationInfo Generate(MethodInfo methodInfo)
        {
            List <ConfigurationPropertyInfo> properties = new List <ConfigurationPropertyInfo>();

            foreach (ParameterInfo pi in methodInfo.GetParameters())
            {
                string        name          = pi.Name;
                NameAttribute nameAttribute = GetAttribute <NameAttribute>(pi);
                if (nameAttribute != null)
                {
                    name = nameAttribute.Name;
                }

                string alias = name;

                AliasAttribute aliasAttribute = GetAttribute <AliasAttribute>(pi);
                if (aliasAttribute != null)
                {
                    alias = aliasAttribute.Alias;
                }

                object defaultValue = null;

                DefaultValueAttribute defaultValueAttribute = GetAttribute <DefaultValueAttribute>(pi);
                if (defaultValueAttribute != null)
                {
                    defaultValue = defaultValueAttribute.DefaultValue;
                }

                SwitchAttribute switchAttribute = GetAttribute <SwitchAttribute>(pi);

                if (switchAttribute != null)
                {
                    bool defaultSwitch = false;
                    if (defaultValue != null)
                    {
                        defaultSwitch = (bool)defaultValueAttribute.DefaultValue;
                    }
                    properties.Add(new ConfigurationSwitchPropertyInfo(name, alias, defaultSwitch));
                }
                else
                {
                    properties.Add(new ConfigurationPropertyInfo(name, alias, defaultValue));
                }
            }

            return(new ConfigurationInfo(properties));
        }
Example #7
0
        //</Snippet2>
        static void Main()
        {
            try
            {
                // Initialize trace switches.
                //<Snippet3>
#if (!ConfigFile)
                TraceSwitch traceSwitch = new TraceSwitch("TraceSwitch", "Verbose");
#endif
                //</Snippet3>
                //<Snippet4>
                Console.WriteLine(traceSwitch.Level);
                //</Snippet4>
                //<Snippet5>
#if (!ConfigFile)
                BooleanSwitch boolSwitch = new BooleanSwitch("BoolSwitch", "True");
#endif
                //</Snippet5>
                //<Snippet6>
                Console.WriteLine(boolSwitch.Enabled);
                //</Snippet6>
                //<Snippet7>
#if (!ConfigFile)
                SourceSwitch sourceSwitch = new SourceSwitch("SourceSwitch", "Verbose");
#endif
                //</Snippet7>
                //<Snippet8>
                Console.WriteLine(sourceSwitch.Level);
                //</Snippet8>
                // Initialize trace source.
                //<Snippet9>
                MyTraceSource ts = new MyTraceSource("TraceTest");
                //</Snippet9>
                //<Snippet10>
                Console.WriteLine(ts.Switch.DisplayName);
                //</Snippet10>
                //<Snippet11>
                Console.WriteLine(ts.Switch.Level);
                //</Snippet11>
                //<Snippet12>
                SwitchAttribute[] switches = SwitchAttribute.GetAll(typeof(TraceTest).Assembly);
                for (int i = 0; i < switches.Length; i++)
                {
                    Console.Write(switches[i].SwitchName);
                    Console.Write("\t");
                    Console.WriteLine(switches[i].SwitchType);
                }
                //</Snippet12>
                //<Snippet13>
                // Display the SwitchLevelAttribute for the BooleanSwitch.
                Object[] attribs = typeof(BooleanSwitch).GetCustomAttributes(typeof(SwitchLevelAttribute), false);
                if (attribs.Length == 0)
                {
                    Console.WriteLine("Error, couldn't find SwitchLevelAttribute on BooleanSwitch.");
                }
                else
                {
                    Console.WriteLine(((SwitchLevelAttribute)attribs[0]).SwitchLevelType.ToString());
                }
                //</Snippet13>
#if (ConfigFile)
                //<Snippet14>
                // Get the custom attributes for the TraceSource.
                Console.WriteLine(ts.Attributes.Count);
                foreach (DictionaryEntry de in ts.Attributes)
                {
                    Console.WriteLine(de.Key + "  " + de.Value);
                }
                //</Snippet14>
                //<Snippet15>
                // Get the custom attributes for the trace source switch.
                foreach (DictionaryEntry de in ts.Switch.Attributes)
                {
                    Console.WriteLine(de.Key + "  " + de.Value);
                }
                //</Snippet15>
#endif
                //<Snippet16>
                ts.Listeners["console"].TraceOutputOptions |= TraceOptions.Callstack;
                //</Snippet16>
                //<Snippet17>
                ts.TraceEvent(TraceEventType.Warning, 1);
                //</Snippet17>
                ts.Listeners["console"].TraceOutputOptions = TraceOptions.DateTime;
                //<Snippet18>
                // Issue file not found message as a warning.
                ts.TraceEvent(TraceEventType.Warning, 1, "File Test not found");
                //</Snippet18>
                System.Threading.Thread.Sleep(1000);
                //<Snippet19>
                Console.WriteLine(ts.Listeners.Count);
                //</Snippet19>
                //<Snippet20>
                ts.Listeners.Add(new TestListener(TESTLISTENERFILE, "TestListener"));
                //</Snippet20>
                Console.WriteLine(ts.Listeners.Count);
                //<Snippet22>
                foreach (TraceListener traceListener in ts.Listeners)
                {
                    Console.Write(traceListener.Name + "\t");
                    // The following output can be used to update the configuration file.
                    Console.WriteLine((traceListener.GetType().AssemblyQualifiedName));
                }
                //</Snippet22>
                //<Snippet23>
                // Show correlation and output options.
                Trace.CorrelationManager.StartLogicalOperation("abc");
                //</Snippet23>
                //<Snippet24>
                // Issue file not found message as a verbose event using a formatted string.
                ts.TraceEvent(TraceEventType.Verbose, 2, "File {0} not found.", "test");
                //</Snippet24>
                Trace.CorrelationManager.StartLogicalOperation("def");
                // Issue file not found message as information.
                ts.TraceEvent(TraceEventType.Information, 3, "File {0} not found.", "test");
                //<Snippet25>
                Trace.CorrelationManager.StopLogicalOperation();
                //</Snippet25>
                //<Snippet26>
                ts.Listeners["console"].TraceOutputOptions |= TraceOptions.LogicalOperationStack;
                //</Snippet26>
                // Issue file not found message as an error event.
                ts.TraceEvent(TraceEventType.Error, 4, "File {0} not found.", "test");
                Trace.CorrelationManager.StopLogicalOperation();
                // Write simple message from trace.
                //<Snippet27>
                Trace.TraceWarning("Warning from Trace.");
                //</Snippet27>
                //<Snippet28>
                // Test the filter on the ConsoleTraceListener.
                ts.Listeners["console"].Filter = new SourceFilter("No match");
                ts.TraceData(TraceEventType.Information, 5,
                             "SourceFilter should reject this message for the console trace listener.");
                ts.Listeners["console"].Filter = new SourceFilter("TraceTest");
                ts.TraceData(TraceEventType.Information, 6,
                             "SourceFilter should let this message through on the console trace listener.");
                //</Snippet28>
                ts.Listeners["console"].Filter = null;
                // Use the TraceData method.
                //<Snippet30>
                ts.TraceData(TraceEventType.Warning, 9, new object());
                //</Snippet30>
                //<Snippet31>
                ts.TraceData(TraceEventType.Warning, 10, new object[] { "Message 1", "Message 2" });
                //</Snippet31>
                // Activity tests.
                //<Snippet32>
                ts.TraceEvent(TraceEventType.Start, 11, "Will not appear until the switch is changed.");
                ts.Switch.Level = SourceLevels.ActivityTracing | SourceLevels.Critical;
                ts.TraceEvent(TraceEventType.Suspend, 12, "Switch includes ActivityTracing, this should appear");
                ts.TraceEvent(TraceEventType.Critical, 13, "Switch includes Critical, this should appear");
                //</Snippet32>

                Trace.Flush();
                ((TextWriterTraceListener)Trace.Listeners["file"]).Close();
                ((TextWriterTraceListener)Trace.Listeners["fileBuilder"]).Close();

                System.Threading.Thread.Sleep(1000);


                // Examine the contents of the log file.
                Console.WriteLine("\n\n\n");
                // Use try/catch in case the file hasn't been defined.
                try
                {
                    Console.WriteLine("Examining " + TEXTFILE);
                    StreamReader logfile = new StreamReader(TEXTFILE);
                    while (!logfile.EndOfStream)
                    {
                        Console.WriteLine(logfile.ReadLine());
                    }

                    logfile.Close();

                    File.Delete(TEXTFILE);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error tying to read the text log file. " + e.Message);
                }

                // Display the contents of the log file builder.
                Console.WriteLine("\n\n\n");
                // Use try/catch in case the file hasn't been defined.
                try
                {
                    Console.WriteLine("Examining " + DELIMITEDFILE);
                    StreamReader logfile = new StreamReader(DELIMITEDFILE);
                    while (!logfile.EndOfStream)
                    {
                        Console.WriteLine(logfile.ReadLine());
                    }
                    logfile.Close();
                    File.Delete(DELIMITEDFILE);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error tying to read the delimited text file. " + e.Message);
                }

                Console.WriteLine("Press any key to exit.");
                Console.Read();
            }
            catch (Exception e)
            {
                // Catch any unexpected exception.
                Console.WriteLine("Unexpected exception: " + e.ToString());
                Console.Read();
            }
        }
        public void ConstructSwitchAttribute()
        {
            SwitchAttribute attribute = new SwitchAttribute("NoLog");

            Assert.IsFalse(attribute.AllowEmptyValues, "Allow empty values is not supported.");
        }