Skip to content

How do I disable logging? #9

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
beppe9000 opened this issue Jul 20, 2016 · 5 comments
Open

How do I disable logging? #9

beppe9000 opened this issue Jul 20, 2016 · 5 comments

Comments

@beppe9000
Copy link

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?

@bonesoul
Copy link
Owner

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

@beppe9000
Copy link
Author

This one, for example:

Logger.InfoFormat("Embedded uhttpserver started.");

@sirkris
Copy link

sirkris commented Feb 6, 2019

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?

@isen-ng
Copy link

isen-ng commented Feb 20, 2019

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;
                }
            }
        }

@robie2011
Copy link

same issue. Workaround didn't work.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants