uhttpsharp
uhttpsharp copied to clipboard
How do I disable logging?
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?
only console.write reference is in the demo project? are you sure the log lines you mention are from uhttpsharp?
This one, for example: https://github.com/bonesoul/uhttpsharp/blob/e3ac7fcf010582c4b2a4061590a8913568f48637/uhttpsharp/HttpServer.cs#L66
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?
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;
}
}
}
same issue. Workaround didn't work.