Unhandled exception handler not called

I'm using the LoadBalancing project (v3.4.31.10808) and I'm trying to register an UnhandledEceptionEventHandler. I want to log unhandled exceptions using a custom log4net appender (SlackAppender).

I know Photon logs unhandled exceptions to PhotonCLR.log, but adding my custom appender to the Photon.local.log4net file but when I did that, nothing logged (presumably because whatever references Photon.local.log4net doesn't have any clue about my appender).

Then, I tried updating the log4net config file (that gets deployed to the LoadBalancing application's directory) to use my appender. The normal logging that config file handles seems to go through, which leads me to believe the issue is not with log4net, but maybe with how I'm trying to capture unhandled exceptions in my code.

I added this line to my MasterApplication#Setup class:

AppDomain.CurrentDomain.UnhandledException += MyExceptionHandler;

That method simply looks like this:
private static void MyExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
    Exception e = (Exception)args.ExceptionObject;
    log.Info("Unhandled exception logger called...", e);
    log.Error("Unhandled exception occurred in our code...", e);
}
However, when I put a breakpoint inside that method, it's never called even when I explicitly throw an exception in CreatePeer of that same class. Is this somehow related to different app domains and attaching my event to the wrong one?

Comments

  • hi, @atraver

    your method is not called because exception is handled. during creation of peer we catch all exceptions and send debug info to client. i see that we missed logging. i will add it.

    More on this. now when you create PoolFiber by default we use FailSafeBatchExecutor, before DefaultExecutor was used. FailSafeBatchExecutor catches exceptions which happened inside executed action and logs them out. we catch right now all this in order to have guaranties that all enqueued actions will be executed.

    btw, Photon also sets up Unhandled exception executor, which logs errors to file. so, if the perpose of your handler is only logging you already have that.

    best,
    ilya
  • Thanks, Ilya.

    Indeed the sole purpose of my exception handler/appender is to log unhandled exceptions. However, I'm trying to include a custom log4net appender (Log4Slack) which is an external package. Is it possible to use an external package as an appender to log unhandled exceptions? When I tried to add this block to my Photon.local.log4net file in the deploy/bin_Win64 directory, nothing seems to happen:
    <appender name="SlackAppender" type="Log4Slack.SlackAppender, Log4Slack">
    	<WebhookUrl value="https://my-slack-webhook-url" />
    	<Channel value="#testing" />
    	<Username value="Log4Slack" />
    	<IconEmoji value=":ghost:" />
    	<AddAttachment value="true" />
    	<AddExceptionTraceField value="true" />
    </appender>
    
    <root>
    	<level value="INFO" />
    	<appender-ref ref="A1" />
    	<appender-ref ref="SlackAppender" />
    </root>
    
    I wonder if it's because at that point, Photon doesn't know about the appender? If I put those lines in the log4net.config file inside Master/bin (using the LoadBalancing app), I get some logging but not the unhandled exceptions, which are critical.
  • i believe you are using wrong log4net.config

    you should use one which in folder deploy/LoadBalancing/Master/bin and deploy/LoadBalancing/GameServer/bin

    best,
    ilya
  • Ah, I think I see what's happening. The errors were getting logged into our GSGame1.log file (and subsequent Slack notifications were being sent), but a few slipped through the cracks, which gave me pause. Notably, we see an error in PhotonCLR.log when we update Photon in-place while one or more peers are connected:

    [{"Message":"2017-06-05 19:57:01,261 [30] ERROR PhotonHostRuntime.PhotonDomainManager - UnhandledException: \nSystem.AppDomainUnloadedException: Attempted to access an unloaded AppDomain.","Time":1496717821261,"Host":"photon_server_dev","Category":"photon_server","Name":"C:\\PhotonServer\\deploy\\bin_Win64\\log\\PhotonCLR.log","Collector":"Photon (dev)"}]

    That error never made its way to Slack, which made me think maybe there were other errors also getting ignored. Since it's not necessarily an error we care much about (although we're also not sure why it happens -- maybe you can give us an idea?) because we only do hot-reloading in our development environment. That made me think not all errors were making their way to the MSMaster.log and GSGame1.log, but in fact I can see unhandled exceptions from our application's domain getting logged in both places.
  • this kind of error may happen in case of realoading of domains. so, if domain of your game application is unloaded than logger is unloaвed too and it can not log to Game.log.

    as a rule, you should see only unhandled exceptions in PhotonCLR.log. all unhandled exceptions are logged to Master.log or GameServer.log too, if this is possible

    best,
    ilya
  • @chvetsov

    as a rule, you should see only unhandled exceptions in PhotonCLR.log. all unhandled exceptions are logged to Master.log or GameServer.log too, if this is possible
    I think you meant:

    UNhandled exceptions ==> PhotonCLR.log
    Handled exceptions ==> MasterServer.log and GameServer.log
  • @JohnTube

    no, no everything is right in my message

    we log unhandled exceptions everywhere. but sometimes we may log them only to PhotonCLR.log, because for instance Master or GS domains are not loaded yet or already unloaded. i think this is the only case for such exception

    best,
    ilya
Sign In or Register to comment.