Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Custom logger not working #553

Open
AgrYpn1a opened this issue Mar 28, 2022 · 2 comments
Open

Custom logger not working #553

AgrYpn1a opened this issue Mar 28, 2022 · 2 comments
Labels
area:core Issue with EmbedIO core (server, modules, etc.) v3.x

Comments

@AgrYpn1a
Copy link

AgrYpn1a commented Mar 28, 2022

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
Copy link
Collaborator

rdeago commented Mar 28, 2022

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>();

@rdeago rdeago added v3.x area:core Issue with EmbedIO core (server, modules, etc.) labels Mar 28, 2022
@AgrYpn1a
Copy link
Author

AgrYpn1a commented Mar 28, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:core Issue with EmbedIO core (server, modules, etc.) v3.x
Projects
None yet
Development

No branches or pull requests

2 participants