uhttpsharp icon indicating copy to clipboard operation
uhttpsharp copied to clipboard

How do I disable logging?

Open beppe9000 opened this issue 8 years ago • 5 comments

I would like to disable the automatic uhttpsharp log in my console application by either some boolean flag or by changing the logger level. Any help?

beppe9000 avatar Jul 20 '16 00:07 beppe9000

only console.write reference is in the demo project? are you sure the log lines you mention are from uhttpsharp?

bonesoul avatar Jul 29 '16 00:07 bonesoul

This one, for example: https://github.com/bonesoul/uhttpsharp/blob/e3ac7fcf010582c4b2a4061590a8913568f48637/uhttpsharp/HttpServer.cs#L66

beppe9000 avatar Jul 29 '16 13:07 beppe9000

Bump! I would really love to know how to disable this annoying message every time I use the library:

Exception occured resolving a log provider. Logging for this assembly uhttpsharp , Version=0.1.5653.28566, Culture=neutral, PublicKeyToken=null is disabled. Syst em.TypeInitializationException: The type initializer for 'uhttpsharp.Logging.Log Providers.ColouredConsoleLogProvider' threw an exception. ---> System.InvalidOpe rationException: System.Console or System.ConsoleColor type not found at uhttpsharp.Logging.LogProviders.ColouredConsoleLogProvider..cctor() in c:
Users\shani\Documents\GitHub\uHttpSharp\uhttpsharp\App_Packages\LibLog.3.1\LibLo g.cs:line 1629 --- End of inner exception stack trace --- at uhttpsharp.Logging.LogProviders.ColouredConsoleLogProvider.IsLoggerAvailab le() in c:\Users\shani\Documents\GitHub\uHttpSharp\uhttpsharp\App_Packages\LibLo g.3.1\LibLog.cs:line 1645 at uhttpsharp.Logging.LogProvider.ResolveLogProvider() in c:\Users\shani\Docu ments\GitHub\uHttpSharp\uhttpsharp\App_Packages\LibLog.3.1\LibLog.cs:line 453

Why can't you just let the exception bubble up so I can catch it?

sirkris avatar Feb 06 '19 06:02 sirkris

Do this for a work around:

        static YourClass()
        {
            LogProvider.LogProviderResolvers.Add(
                new Tuple<LogProvider.IsLoggerAvailable, LogProvider.CreateLogProvider>(() => true,
                    () => NullLoggerProvider.Instance));
        }

        public class NullLoggerProvider : ILogProvider
        {
            public static readonly NullLoggerProvider Instance = new NullLoggerProvider();
            
            private static readonly ILog NullLogInstance = new NullLog();
            
            public ILog GetLogger(string name)
            {
                return NullLogInstance;
            }

            public IDisposable OpenNestedContext(string message)
            {
                return null;
            }

            public IDisposable OpenMappedContext(string key, string value)
            {
                return null;
            }
            
            public class NullLog : ILog
            {
                public bool Log(LogLevel logLevel, Func<string> messageFunc, Exception exception = null, params object[] formatParameters)
                {
                    // do nothing
                    return true;
                }
            }
        }

isen-ng avatar Feb 20 '19 04:02 isen-ng

same issue. Workaround didn't work.

robie2011 avatar May 26 '20 05:05 robie2011