1

I am using: WebSocketSharp - https://github.com/sta/websocket-sharp

And the following code as part of my p2pserver:

public void Listen()
{
    var server = new WebSocketServer(P2PPort);

    server.Log.Output += (d, s) => { };
    server.AddWebSocketService<P2PServerSocketBehavior>("/", bhv =>
    {
        bhv.OnConnection += (s, e) => ConnectSocket(e.Socket);
        bhv.OnException += (s, e) => CloseSocket(e.Socket, e.Message);
        bhv.OnDisconnect += (s, e) => CloseSocket(e.Socket, e.Reason);
    });

    server.Log.Output += (d, s) => { };
    server.Start();
    server.Log.Output += (d, s) => { };

    Console.WriteLine($"Listening for peer-to-peer connections on port {P2PPort}");

    ConnectToPeers();
}

private void ConnectToPeers()
{
    PeerAddresses.ForEach(peer =>
    {
        var socket = new WebSocket(peer);
        socket.Log.Output += (d, s) => { };
        socket.OnOpen += (s, e) => ConnectSocket(socket);
        socket.OnError += (s, e) => CloseSocket(socket, e.Message);
        socket.OnClose += (s, e) => CloseSocket(socket, e.Reason);
        socket.Log.Output += (d, s) => { };
        socket.Connect();
        socket.Log.Output += (d, s) => { };
    });
}

As you can see above I am trying to disable console logging as described here: https://github.com/sta/websocket-sharp/issues/368

Despite that fact I am still receiving error logs:

enter image description here

I have isolated the issue to this code inside websocket-sharp:

private static void defaultOutput(LogData data, string path)
{
    var log = data.ToString();
    Console.WriteLine(log);
    if (path != null && path.Length > 0)
        writeToFile(log, path);
}

private void output(string message, LogLevel level)
{
    lock (_sync)
    {
        if (_level > level)
            return;

        LogData data = null;
        try
        {
            data = new LogData(level, new StackFrame(2, true), message);
            _output(data, _file);
        }
        catch (Exception ex)
        {
            data = new LogData(LogLevel.Fatal, new StackFrame(0, true), ex.Message);
            Console.WriteLine(data.ToString());
        }
    }
}

Commenting 'Console.WriteLine' in the first function solves the problem.

So according to the above it seems like for some reason '_output' remains 'defaultOutput' despite explicitly setting its value.

My requirement is to be able to disable logging while using Nuget package of WebSocketSharp (so I can't really comment the internal code). I guess that I am missing something really simple here.

// EDIT 1

If I handle the message to actually send output to the console, it appears twice so it means that the default action is not replaced.

server.Log.Output += (d, s) =>
{
    Console.WriteLine(d.ToString());
};
rvnlord
  • 3,487
  • 3
  • 23
  • 32

2 Answers2

1

I solved it the hacky way:

public static void Disable(this Logger logger)
{
    var field = logger.GetType().GetField("_output", BindingFlags.NonPublic | BindingFlags.Instance);
    field?.SetValue(logger, new Action<LogData, string>((d, s) => { }));
}
rvnlord
  • 3,487
  • 3
  • 23
  • 32
0

From GitHub: https://github.com/sta/websocket-sharp

#if DEBUG
      // To change the logging level.
      httpsv.Log.Level = LogLevel.Trace;
#endif
ggeorge
  • 1,496
  • 2
  • 13
  • 19