unosquare / embedio

A tiny, cross-platform, module based web server for .NET
http://unosquare.github.io/embedio
Other
1.45k stars 175 forks source link

Custom logger not working #553

Open AgrYpn1a opened 2 years ago

AgrYpn1a commented 2 years ago

I tried implementing a custom logger:

  public class ServerLogger : Swan.Logging.ILogger
  {
      public Swan.Logging.LogLevel LogLevel =>
          Swan.Logging.LogLevel.Info | Swan.Logging.LogLevel.Error | Swan.Logging.LogLevel.Trace | Swan.Logging.LogLevel.Debug | Swan.Logging.LogLevel.Warning;

      public void Dispose()
      {
          throw new NotImplementedException();
      }

      public void Log(Swan.Logging.LogMessageReceivedEventArgs logEvent)
      {
          Debug.Log("<color=yellow>Called logger</color>");
          ImGuiLogger.Instance.Log("Calling log from custom logger.");
          ImGuiLogger.Instance.Log(logEvent.Message);
      }
  }

and then I tried to register it

    string url = "https://127.0.0.1:6060/";
    string certPath = $"File://{Application.streamingAssetsPath}/cert.pfx";
    var cert = new X509Certificate2(new X509Certificate($"{Application.streamingAssetsPath}/cert.pfx"));
    _server = new WebServer(o =>
        o.WithUrlPrefix(url)
        .WithMode(HttpListenerMode.EmbedIO)
        .WithCertificate(cert)
    ).WithLocalSessionManager()
    .WithWebApi("/THLinkAPI", (m) => m.WithController<THLinkApiController>());
    Swan.Logging.Logger.RegisterLogger<ServerLogger>();

But I am not able to see any outputs. Log method is never called. Am I missing something?

Using this from Unity 2020 on .NET 2.0 standard.

BTW, it's on running on Unitys Mono, and I am having SSL connection error which I am trying to debug, is why I need custom logger, cause I can't see any logs from Unity. If possibly I did something wrong here with setting up SSL you can help me correct that, then I wouldn't care about logs further unless I have another issue at some point.

rdeago commented 2 years ago

Hello @rtojagic, thanks for using EmbedIO!

Your logger is not working because its LogLevel property is greater than any log level. Don't get fooled by the LogLevel enum having a [Flags] attribute, it's a long-standing bug in Swan.Lite.

On a side note, if you don't need to dispose your logger just make Dispose an empty method. Trowing an exception could (and probably will) disrupt Logger's shutdown procedure.

  public class ServerLogger : Swan.Logging.ILogger
  {
      public Swan.Logging.LogLevel LogLevel => Swan.Logging.LogLevel.Trace;

      public void Dispose()
      {
      }

      public void Log(Swan.Logging.LogMessageReceivedEventArgs logEvent)
      {
          Debug.Log("<color=yellow>Called logger</color>");
          ImGuiLogger.Instance.Log("Calling log from custom logger.");
          ImGuiLogger.Instance.Log(logEvent.Message);
      }
  }

Regarding the SSL connection error, if your program runs under Windows you probably need to register your certificate with the OS security subsystem. EmbedIO can do it for you:

    string url = "https://127.0.0.1:6060/";
    string certPath = $"File://{Application.streamingAssetsPath}/cert.pfx";
    var cert = new X509Certificate2(new X509Certificate(certPath));
    _server = new WebServer(o =>
        o.WithUrlPrefix(url)
        .WithMode(HttpListenerMode.EmbedIO)
        .WithCertificate(cert)
        .WithAutoRegisterCertificate() // <-- Add this
    ).WithLocalSessionManager()
    .WithWebApi("/THLinkAPI", (m) => m.WithController<THLinkApiController>());
    Swan.Logging.Logger.RegisterLogger<ServerLogger>();
AgrYpn1a commented 2 years ago

@rdeago Hey thanks a lot for the fast reply! :) I've set the log level to Trace and I can see logs now which is awesome.

Btw, now when I add the WithAutoRegisterCertificate I am getting a nullptr exception, do you know why that could happen? image

And, btw, certificate is already installed: image

If I omit the WithAutoRegisterCertificate, and test from Insomnia I would get the SSL Connect error, with timeline details as follows. I do not see any logs, so I believe it's not even reaching that part of the server app. image