ArgUtils

Namespace: Common.Logging
We found 10 examples in language CSharp for this search. You will see 30 fragments of code.
        [STAThread]
        static void Main(string[] args) {
            CreateCommandlineParameters(args);
            //string[] ar = { "-c", "somelocation", "-d"};
            //args = ar;
            ArgUtils.Handle(args);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
        private static void debugParameter(object sender, EventArgs e) {
            ArgUtils.ArgHandlerParams p = (ArgUtils.ArgHandlerParams)e;
            string st = "";
            foreach (string s in p.parameters) st += s + " ";
            MessageBox.Show(string.Format("{0} {1} {2}", p.handler.flag, p.parameters.Count.ToString(), st));
        }


        protected override ILog CreateLogger(string name)
        {
            return new UnityDebugLogger(name, _level, _showLogName, _showLogLevel, _useUnityLogLevel);
        }


        /// <summary>
        /// Create the specified logger instance
        /// </summary>
        protected override ILog CreateLogger(string name)
        {
            return CreateLogger(name, _level, _showLevel, _showDateTime, _showLogName, _dateTimeFormat);
        }

        /// <summary>
        /// Derived factories need to implement this method to create the
        /// actual logger instance.
        /// </summary>
        /// <returns>a new logger instance. Must never be <c>null</c>!</returns>
        protected abstract ILog CreateLogger(string name, LogLevel level, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat);

        [Test]
        public void GetValue()
        {
            NameValueCollection nvc = new NameValueCollection();
            nvc["key"] = "value";

            Assert.AreEqual( null,  ArgUtils.GetValue(null, "key"));
            Assert.AreEqual("value", ArgUtils.GetValue(nvc, "key"));
            Assert.AreEqual(null, ArgUtils.GetValue(nvc, "wrongkey"));
            Assert.AreEqual("defaultValue", ArgUtils.GetValue(null, "wrongkey", "defaultValue"));
            Assert.AreEqual("defaultValue", ArgUtils.GetValue(nvc, "wrongkey", "defaultValue"));
        }

        [Test]
        public void Coalesce()
        {
            Assert.AreEqual(null, ArgUtils.Coalesce());
            Assert.AreEqual(null, ArgUtils.Coalesce(null, null));
            Assert.AreEqual("x", ArgUtils.Coalesce(string.Empty, null, "x"));
            // null predicate causes the use the default predicate of (v!=null)
            Assert.AreEqual(string.Empty, ArgUtils.Coalesce( (Predicate<string>)null, string.Empty, (string)null, "x"));
            Assert.AreEqual(null, ArgUtils.Coalesce<object>( delegate(object v) { return v != null; } ));
            Assert.AreEqual(string.Empty, ArgUtils.Coalesce<object>( delegate(object v) { return v != null; }, null, string.Empty, "x"));
        }

        [Test]
        public void TryParseEnum()
        {
            Assert.Throws( 
                Is.TypeOf<ArgumentException>().And.Message.EqualTo( string.Format("Type '{0}' is not an enum type", typeof(int).FullName) )
                , delegate
                    {
                     ArgUtils.TryParseEnum((int) 1, "0");
                    }
                );

            Assert.AreEqual( LogLevel.Fatal, ArgUtils.TryParseEnum(LogLevel.All, "fatal") );
            Assert.AreEqual( LogLevel.Debug, ArgUtils.TryParseEnum(LogLevel.Debug, "invalid value") );
            Assert.AreEqual( LogLevel.Debug, ArgUtils.TryParseEnum(LogLevel.Debug, null) );
        }

        [Test]
        public void TryParse()
        {
            Assert.Throws( 
                Is.TypeOf<ArgumentException>()
                .And.Message.EqualTo(string.Format("There is no parser registered for type {0}", typeof(object).FullName))
                , delegate
                    {
                     ArgUtils.TryParse(new object(), "0");
                    }
                );

            Assert.AreEqual( true, ArgUtils.TryParse(false, "trUE") );
            Assert.AreEqual( 1, ArgUtils.TryParse(2, "1") );
            Assert.AreEqual( 2, ArgUtils.TryParse(2, "2invalidnumber1") );
            Assert.AreEqual( (short)1, ArgUtils.TryParse((short)2, "1") );
            Assert.AreEqual( (long)1, ArgUtils.TryParse((long)2, "1") );
            Assert.AreEqual( (float)1, ArgUtils.TryParse((float)2, "1") );
            Assert.AreEqual( (double)1, ArgUtils.TryParse((double)2, "1") );
            Assert.AreEqual( (decimal)1, ArgUtils.TryParse((decimal)2, "1") );
        }

        [Test]
        public void AssertIsAssignable()
        {
            Assert.Throws(
                Is.TypeOf<ArgumentNullException>()
                    .And.Message.EqualTo(new ArgumentNullException("valType").Message)
                , delegate
                    {
                        ArgUtils.AssertIsAssignable<IConvertible>("arg", null);
                    }
                );

            Assert.Throws(
                Is.TypeOf<ArgumentOutOfRangeException>()
                    .And.Message.EqualTo(new ArgumentOutOfRangeException("this", this.GetType(),string.Format("Type '{0}' of parameter '{1}' is not assignable to target type '{2}'"
                                               , this.GetType().AssemblyQualifiedName
                                               , "this"
                                               , typeof (ISerializable).AssemblyQualifiedName) ).Message)
                , delegate
                    {
                        ArgUtils.AssertIsAssignable<ISerializable>("this", this.GetType());
                    }
                );

            Type type = typeof(Int32);
            Assert.AreSame(type, ArgUtils.AssertIsAssignable<IConvertible>("arg", type));
        }

        [Test]
        public void AssertNotNullThrowsArgumentNullException()
        {
            object tmp = new object();
            Assert.AreSame(tmp, ArgUtils.AssertNotNull("arg", tmp));
            Assert.Throws(Is.TypeOf<ArgumentNullException>()
                          .And.Message.EqualTo(new ArgumentNullException("tmp").Message),
                          delegate { ArgUtils.AssertNotNull("tmp", (object)null); });
            Assert.Throws(Is.TypeOf<ArgumentNullException>().And.Message.EqualTo(new ArgumentNullException("tmp", "message msgarg").Message),
                          delegate { ArgUtils.AssertNotNull("tmp", (object)null, "message {0}", "msgarg"); });
        }

        [Test]
        public void GetValue()
        {
            NameValueCollection nvc = new NameValueCollection();
            nvc["key"] = "value";

            Assert.AreEqual( null,  ArgUtils.GetValue(null, "key"));
            Assert.AreEqual("value", ArgUtils.GetValue(nvc, "key"));
            Assert.AreEqual(null, ArgUtils.GetValue(nvc, "wrongkey"));
            Assert.AreEqual("defaultValue", ArgUtils.GetValue(null, "wrongkey", "defaultValue"));
            Assert.AreEqual("defaultValue", ArgUtils.GetValue(nvc, "wrongkey", "defaultValue"));
        }

        [Test]
        public void Coalesce()
        {
            Assert.AreEqual(null, ArgUtils.Coalesce());
            Assert.AreEqual(null, ArgUtils.Coalesce(null, null));
            Assert.AreEqual("x", ArgUtils.Coalesce(string.Empty, null, "x"));
            // null predicate causes the use the default predicate of (v!=null)
            Assert.AreEqual(string.Empty, ArgUtils.Coalesce( (Predicate<string>)null, string.Empty, (string)null, "x"));
            Assert.AreEqual(null, ArgUtils.Coalesce<object>( delegate(object v) { return v != null; } ));
            Assert.AreEqual(string.Empty, ArgUtils.Coalesce<object>( delegate(object v) { return v != null; }, null, string.Empty, "x"));
        }

        [Test]
        public void TryParseEnum()
        {
            Assert.Throws( 
                Is.TypeOf<ArgumentException>().And.Message.EqualTo( string.Format("Type '{0}' is not an enum type", typeof(int).FullName) )
                , delegate
                    {
                     ArgUtils.TryParseEnum((int) 1, "0");
                    }
                );

            Assert.AreEqual( LogLevel.Fatal, ArgUtils.TryParseEnum(LogLevel.All, "fatal") );
            Assert.AreEqual( LogLevel.Debug, ArgUtils.TryParseEnum(LogLevel.Debug, "invalid value") );
            Assert.AreEqual( LogLevel.Debug, ArgUtils.TryParseEnum(LogLevel.Debug, null) );
        }

        [Test]
        public void TryParse()
        {
            Assert.Throws( 
                Is.TypeOf<ArgumentException>()
                .And.Message.EqualTo(string.Format("There is no parser registered for type {0}", typeof(object).FullName))
                , delegate
                    {
                     ArgUtils.TryParse(new object(), "0");
                    }
                );

            Assert.AreEqual( true, ArgUtils.TryParse(false, "trUE") );
            Assert.AreEqual( 1, ArgUtils.TryParse(2, "1") );
            Assert.AreEqual( 2, ArgUtils.TryParse(2, "2invalidnumber1") );
            Assert.AreEqual( (short)1, ArgUtils.TryParse((short)2, "1") );
            Assert.AreEqual( (long)1, ArgUtils.TryParse((long)2, "1") );
            Assert.AreEqual( (float)1, ArgUtils.TryParse((float)2, "1") );
            Assert.AreEqual( (double)1, ArgUtils.TryParse((double)2, "1") );
            Assert.AreEqual( (decimal)1, ArgUtils.TryParse((decimal)2, "1") );
        }

        [Test]
        public void AssertIsAssignable()
        {
            Assert.Throws(
                Is.TypeOf<ArgumentNullException>()
                    .And.Message.EqualTo(new ArgumentNullException("valType").Message)
                , delegate
                    {
                        ArgUtils.AssertIsAssignable<IConvertible>("arg", null);
                    }
                );

            Assert.Throws(
                Is.TypeOf<ArgumentOutOfRangeException>()
                    .And.Message.EqualTo(new ArgumentOutOfRangeException("this", this.GetType(),string.Format("Type '{0}' of parameter '{1}' is not assignable to target type '{2}'"
                                               , this.GetType().AssemblyQualifiedName
                                               , "this"
                                               , typeof (ISerializable).AssemblyQualifiedName) ).Message)
                , delegate
                    {
                        ArgUtils.AssertIsAssignable<ISerializable>("this", this.GetType());
                    }
                );

            Type type = typeof(Int32);
            Assert.AreSame(type, ArgUtils.AssertIsAssignable<IConvertible>("arg", type));
        }

        [Test]
        public void AssertNotNullThrowsArgumentNullException()
        {
            object tmp = new object();
            Assert.AreSame(tmp, ArgUtils.AssertNotNull("arg", tmp));
            Assert.Throws(Is.TypeOf<ArgumentNullException>()
                          .And.Message.EqualTo(new ArgumentNullException("tmp").Message),
                          delegate { ArgUtils.AssertNotNull("tmp", (object)null); });
            Assert.Throws(Is.TypeOf<ArgumentNullException>().And.Message.EqualTo(new ArgumentNullException("tmp", "message msgarg").Message),
                          delegate { ArgUtils.AssertNotNull("tmp", (object)null, "message {0}", "msgarg"); });
        }


        /// <summary>
        /// Create the specified logger instance
        /// </summary>
        protected override ILog CreateLogger(string name)
        {
            return CreateLogger(name, _level, _showLevel, _showDateTime, _showLogName, _dateTimeFormat);
        }

        /// <summary>
        /// Derived factories need to implement this method to create the
        /// actual logger instance.
        /// </summary>
        /// <returns>a new logger instance. Must never be <c>null</c>!</returns>
        protected abstract ILog CreateLogger(string name, LogLevel level, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat);


        /// <summary>
        /// Create the specified logger instance
        /// </summary>
        protected override ILog CreateLogger(string name)
        {
            return CreateLogger(name, _level, _showLevel, _showDateTime, _showLogName, _dateTimeFormat);
        }

        /// <summary>
        /// Derived factories need to implement this method to create the
        /// actual logger instance.
        /// </summary>
        /// <returns>a new logger instance. Must never be <c>null</c>!</returns>
        protected abstract ILog CreateLogger(string name, LogLevel level, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat);


        /// <summary>
        /// Create the specified logger instance
        /// </summary>
        protected override ILog CreateLogger(string name)
        {
            return CreateLogger(name, _level, _showLevel, _showDateTime, _showLogName, _dateTimeFormat);
        }

        /// <summary>
        /// Derived factories need to implement this method to create the
        /// actual logger instance.
        /// </summary>
        /// <returns>a new logger instance. Must never be <c>null</c>!</returns>
        protected abstract ILog CreateLogger(string name, LogLevel level, bool showLevel, bool showDateTime, bool showLogName, string dateTimeFormat);

namespace Common.Logging.Configuration
{
    using Common.Logging;
    using System;

    public class LogSetting
    {
        private readonly Type _factoryAdapterType;
        private readonly NameValueCollection _properties;

        public LogSetting(Type factoryAdapterType, NameValueCollection properties)
        {
            ArgUtils.AssertNotNull<Type>("factoryAdapterType", factoryAdapterType);
            ArgUtils.AssertIsAssignable<ILoggerFactoryAdapter>("factoryAdapterType", factoryAdapterType, "Type {0} does not implement {1}", new object[] { factoryAdapterType.AssemblyQualifiedName, typeof(ILoggerFactoryAdapter).FullName });
            this._factoryAdapterType = factoryAdapterType;
            this._properties = properties;
        }

        public Type FactoryAdapterType =>
            this._factoryAdapterType;

        public NameValueCollection Properties =>
            this._properties;
    }
}


        [Test]
        public void GetValue()
        {
            NameValueCollection nvc = new NameValueCollection();
            nvc["key"] = "value";

            Assert.AreEqual( null,  ArgUtils.GetValue(null, "key"));
            Assert.AreEqual("value", ArgUtils.GetValue(nvc, "key"));
            Assert.AreEqual(null, ArgUtils.GetValue(nvc, "wrongkey"));
            Assert.AreEqual("defaultValue", ArgUtils.GetValue(null, "wrongkey", "defaultValue"));
            Assert.AreEqual("defaultValue", ArgUtils.GetValue(nvc, "wrongkey", "defaultValue"));
        }

        [Test]
        public void Coalesce()
        {
            Assert.AreEqual(null, ArgUtils.Coalesce());
            Assert.AreEqual(null, ArgUtils.Coalesce(null, null));
            Assert.AreEqual("x", ArgUtils.Coalesce(string.Empty, null, "x"));
            // null predicate causes the use the default predicate of (v!=null)
            Assert.AreEqual(string.Empty, ArgUtils.Coalesce( (Predicate<string>)null, string.Empty, (string)null, "x"));
            Assert.AreEqual(null, ArgUtils.Coalesce<object>( delegate(object v) { return v != null; } ));
            Assert.AreEqual(string.Empty, ArgUtils.Coalesce<object>( delegate(object v) { return v != null; }, null, string.Empty, "x"));
        }

        [Test]
        public void TryParseEnum()
        {
            Assert.Throws( 
                Is.TypeOf<ArgumentException>().And.Message.EqualTo( string.Format("Type '{0}' is not an enum type", typeof(int).FullName) )
                , delegate
                    {
                     ArgUtils.TryParseEnum((int) 1, "0");
                    }
                );

            Assert.AreEqual( LogLevel.Fatal, ArgUtils.TryParseEnum(LogLevel.All, "fatal") );
            Assert.AreEqual( LogLevel.Debug, ArgUtils.TryParseEnum(LogLevel.Debug, "invalid value") );
            Assert.AreEqual( LogLevel.Debug, ArgUtils.TryParseEnum(LogLevel.Debug, null) );
        }

        [Test]
        public void TryParse()
        {
            Assert.Throws( 
                Is.TypeOf<ArgumentException>()
                .And.Message.EqualTo(string.Format("There is no parser registered for type {0}", typeof(object).FullName))
                , delegate
                    {
                     ArgUtils.TryParse(new object(), "0");
                    }
                );

            Assert.AreEqual( true, ArgUtils.TryParse(false, "trUE") );
            Assert.AreEqual( 1, ArgUtils.TryParse(2, "1") );
            Assert.AreEqual( 2, ArgUtils.TryParse(2, "2invalidnumber1") );
            Assert.AreEqual( (short)1, ArgUtils.TryParse((short)2, "1") );
            Assert.AreEqual( (long)1, ArgUtils.TryParse((long)2, "1") );
            Assert.AreEqual( (float)1, ArgUtils.TryParse((float)2, "1") );
            Assert.AreEqual( (double)1, ArgUtils.TryParse((double)2, "1") );
            Assert.AreEqual( (decimal)1, ArgUtils.TryParse((decimal)2, "1") );
        }

        [Test]
        public void AssertIsAssignable()
        {
            Assert.Throws(
                Is.TypeOf<ArgumentNullException>()
                    .And.Message.EqualTo(new ArgumentNullException("valType").Message)
                , delegate
                    {
                        ArgUtils.AssertIsAssignable<IConvertible>("arg", null);
                    }
                );

            Assert.Throws(
                Is.TypeOf<ArgumentOutOfRangeException>()
                    .And.Message.EqualTo(new ArgumentOutOfRangeException("this", this.GetType(),string.Format("Type '{0}' of parameter '{1}' is not assignable to target type '{2}'"
                                               , this.GetType().AssemblyQualifiedName
                                               , "this"
                                               , typeof (ISerializable).AssemblyQualifiedName) ).Message)
                , delegate
                    {
                        ArgUtils.AssertIsAssignable<ISerializable>("this", this.GetType());
                    }
                );

            Type type = typeof(Int32);
            Assert.AreSame(type, ArgUtils.AssertIsAssignable<IConvertible>("arg", type));
        }

        [Test]
        public void AssertNotNullThrowsArgumentNullException()
        {
            object tmp = new object();
            Assert.AreSame(tmp, ArgUtils.AssertNotNull("arg", tmp));
            Assert.Throws(Is.TypeOf<ArgumentNullException>()
                          .And.Message.EqualTo(new ArgumentNullException("tmp").Message),
                          delegate { ArgUtils.AssertNotNull("tmp", (object)null); });
            Assert.Throws(Is.TypeOf<ArgumentNullException>().And.Message.EqualTo(new ArgumentNullException("tmp", "message msgarg").Message),
                          delegate { ArgUtils.AssertNotNull("tmp", (object)null, "message {0}", "msgarg"); });
        }

Common.Logging.Configuration.ArgUtils : Object

Methods :

public static Void RegisterTypeParser(ParseHandler<T> parser = )
public static String GetValue(NameValueCollection values = , String key = )
public static String GetValue(NameValueCollection values = , String key = , String defaultValue = )
public static String Coalesce(String[] values = )
public static T Coalesce(Predicate<T> predicate = , T[] values = )
public static T TryParseEnum(T defaultValue = , String stringValue = )
public static T TryParse(T defaultValue = , String stringValue = )
public static T AssertNotNull(String paramName = , T val = )
public static T AssertNotNull(String paramName = , T val = , String messageFormat = , Object[] args = )
public static Type AssertIsAssignable(String paramName = , Type valType = )
public static Type AssertIsAssignable(String paramName = , Type valType = , String messageFormat = , Object[] args = )
public static Void Guard(Action action = , String messageFormat = , Object[] args = )
public static T Guard(Function<T> function = , String messageFormat = , Object[] args = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()