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

Registering logger does nothing #302

Open
Wowhere opened this issue Dec 7, 2024 · 0 comments
Open

Registering logger does nothing #302

Wowhere opened this issue Dec 7, 2024 · 0 comments

Comments

@Wowhere
Copy link

Wowhere commented Dec 7, 2024

Describe the bug
After registering logger it is not log anything

To Reproduce

  1. Register logger
  2. Run http server from EmbedIO
  3. Send requests to srsong locations (404 error)

Result: Log is empty. Debugging shows that Log method dont invoked
(Original code https://github.com/Wowhere/SimpleServers/blob/main/Models/HttpServer.cs)
Steps to reproduce the behavior:

using System;
using System.Threading;
using EmbedIO;
using EmbedIO.Actions;
using EmbedIO.WebApi;
using EmbedIO.Files;
using EmbedIO.Authentication;
using EmbedIO.Routing;
using System.Collections.Generic;
using ReactiveUI;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
using Swan;
using System.IO;
using System.Text;

namespace simpleserver.Models
{
    public class ServerLogger : Swan.Logging.ILogger
    {
        public Swan.Logging.LogLevel LogLevel =>
            Swan.Logging.LogLevel.Debug;
        private MemoryStream memoryStream = new MemoryStream() {};
        private StreamWriter logWriter;
        //private StreamReader logReader;
        public void Dispose()
        {
            throw new NotImplementedException();
        }
        public ServerLogger()
        {
            logWriter = new StreamWriter(memoryStream);
        }
        public void Log(Swan.Logging.LogMessageReceivedEventArgs logEvent)
        {
            using (logWriter)
            {
                logWriter.WriteLine(logEvent.CallerLineNumber);
                logWriter.WriteLine(logEvent.UtcDate);
                logWriter.WriteLine(logEvent.Message);
                logWriter.WriteLine(logEvent.Exception);
            }
            //Debug.Write(logEvent.ToJson());
        }
        public string GetCapturedLogs()
        {
            string line = "";
            using (var reader = new StreamReader(memoryStream))
            {
                line = reader.ReadToEnd();
            }
            return line;
        }
    }
    public class TestController : WebApiController
    {
        [Route(HttpVerbs.Get, "/_test")]
        public string Test() {
            HttpContext.Response.Headers.Clear();
            HttpContext.Response.Headers.Add("Server: Test Server");
            return "Test OK";
        }
        public TestController() {
        }

    }
    public class HttpServerRunner : ReactiveObject
    {
        public string port = "8080";
        public string folder = "";
        public string Status = "Init";
        public string Log = "";
        public bool IsLaunched = false;
        public bool IsSecure = false;
        CancellationTokenSource ctSource;
        private WebServer server;
        public HttpServerRunner()
        {
        }
        public void Start()
        {
            try
            {
                
                ctSource = new CancellationTokenSource();
                //string certPath = $"File://{Application.streamingAssetsPath}/cert.pfx";
                //var cert = new X509Certificate2(new X509Certificate($"{Application.streamingAssetsPath}/cert.pfx"));
                Swan.Logging.Logger.RegisterLogger<ServerLogger>();
                server = new WebServer(o => o
                    .WithUrlPrefix("http://*:" + port)
                    .WithMode(HttpListenerMode.EmbedIO))
                    .WithLocalSessionManager()
                    .WithStaticFolder("/", folder, true, m => m.WithContentCaching())
                    .WithModule(new ActionModule("/", HttpVerbs.Any, ctx => ctx.SendDataAsync(new { Message = "Get Index page" })))
                    .WithWebApi("/_test", m => m.WithController<TestController>());

                //var options = new WebServerOptions()
                //    .WithCertificate()
                //    .WithAutoRegisterCertificate(true);
                //Swan.Logging.Logger.RegisterLogger<ServerLogger>();
                Status = "Running";
                ServerLogger logger = new ServerLogger();
                Log = logger.GetCapturedLogs();
                IsLaunched = true;
                server.RunAsync(ctSource.Token).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                IsLaunched = false;
                Status = ex.Message.ToString();
            }
        }
        public void Stop()
        {
            ctSource.Cancel();
            IsLaunched = false;
            Status = "Stopped";
        }
    }
}

Expected behavior
Log have data

Desktop (please complete the following information):

  • OS: Windows 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant