How to debug server code
Couple of methods I came up with for debugging server code. Hope it will help someone to save up some time. :-) Btw I'm using Visual Studio 2008.
First, be sure to setup your Visual Studio project to update server's files automatically. There are two ways to achieve this: 1) go to your server project properties, and in Build tab select Output path to point to the path were Photon is loading your dlls (for example: "e:\Projects\Photon\Server\deploy\Mmo\Photon.MmoDemo.Server\bin\"). 2) Another way to do this is to go to Build Events and add a post build event to copy the files there. Something like: "copy "$(TargetDir)" "$(SolutionDir)\..\..\deploy\Mmo\Photon.MmoDemo.Server\bin\"", also don't forget to choose Run the post-build event: When the build updates the project output. Setup Photon to automatically load changed dlls, and after you hit F5 or Ctrl+Shift+B in Visual Studio, the files will end up were they should be, and Photon will restart the server with your new code.
Now for the debugging part, there are 3 methods. But before debugging make sure Photon is using 2.0 version of .Net runtime! To do this edit your PhotonSocketServer.config file and for instance config add this:
1. Attaching debugger.
Go to Debug -> Attach to Process, select PhotonSocketServer.exe. Also for Attach to: select only Managed code. If you select Native code, then detaching debugger will kill server process. After that hit Attach, and there you go.
2. Launching debugger from your code.
In your Application class (the one that inherits Photon.SocketServer.Application) override Setup() method and add this line: System.Diagnostics.Debugger.Launch(); . When your server code will be launched you get a nice message box that will ask you if you want to attach the debugger. You can debug startup code this way!
3. Attaching debugger automatically.
This one is my favorite, as it attaches the debugger by simply clicking F5 in Visual Studio. For this create a dummy empty C++ project in your solution. Add main.cpp and a stub for main(). Select it as startup project. Setup project dependencies that building this one will build your server too. Go to project properties and in Debugging tab change Command to where PhotonSocketServer.exe is located, for example: "e:\Projects\Photon\Server\deploy\bin_Win32\PhotonSocketServer.exe". For Attach choose Yes. Set Debugger Type to Managed Only (mixing with native kills the server after detaching debugger). And voila! Hit F5 and it will compile, update your server, and attach a debugger in just a couple seconds. :-) And you can debug startup code this way too!
First, be sure to setup your Visual Studio project to update server's files automatically. There are two ways to achieve this: 1) go to your server project properties, and in Build tab select Output path to point to the path were Photon is loading your dlls (for example: "e:\Projects\Photon\Server\deploy\Mmo\Photon.MmoDemo.Server\bin\"). 2) Another way to do this is to go to Build Events and add a post build event to copy the files there. Something like: "copy "$(TargetDir)" "$(SolutionDir)\..\..\deploy\Mmo\Photon.MmoDemo.Server\bin\"", also don't forget to choose Run the post-build event: When the build updates the project output. Setup Photon to automatically load changed dlls, and after you hit F5 or Ctrl+Shift+B in Visual Studio, the files will end up were they should be, and Photon will restart the server with your new code.
Now for the debugging part, there are 3 methods. But before debugging make sure Photon is using 2.0 version of .Net runtime! To do this edit your PhotonSocketServer.config file and for instance config add this:
<Runtime
Assembly="PhotonHostRuntime, Version=2.0.0.0, Culture=neutral"
Type="PhotonHostRuntime.PhotonDomainManager"
CLRVersion="v2.0">
</Runtime>
1. Attaching debugger.
Go to Debug -> Attach to Process, select PhotonSocketServer.exe. Also for Attach to: select only Managed code. If you select Native code, then detaching debugger will kill server process. After that hit Attach, and there you go.
2. Launching debugger from your code.
In your Application class (the one that inherits Photon.SocketServer.Application) override Setup() method and add this line: System.Diagnostics.Debugger.Launch(); . When your server code will be launched you get a nice message box that will ask you if you want to attach the debugger. You can debug startup code this way!
3. Attaching debugger automatically.
This one is my favorite, as it attaches the debugger by simply clicking F5 in Visual Studio. For this create a dummy empty C++ project in your solution. Add main.cpp and a stub for main(). Select it as startup project. Setup project dependencies that building this one will build your server too. Go to project properties and in Debugging tab change Command to where PhotonSocketServer.exe is located, for example: "e:\Projects\Photon\Server\deploy\bin_Win32\PhotonSocketServer.exe". For Attach choose Yes. Set Debugger Type to Managed Only (mixing with native kills the server after detaching debugger). And voila! Hit F5 and it will compile, update your server, and attach a debugger in just a couple seconds. :-) And you can debug startup code this way too!
1
Comments
We should add this post to the docs.
Thanks for sharing!
1. Open solution with your server app, set project's build path to correct one within Photon app
2. Right click solution > Add > Existing Project...
3. Choose PhotonSocketServer.exe
4. Right click and set it as startup project
5. Right click again and go to properties
5a. Set arguments to "/debug Instance1" without quotation
5b. Set debugger type to Managed
6. Hit F5 and you are rolling right from very first line of managed code 8-).
Note: as it runs completely under hood of Visual Studio when you stop debugging it will also stop server. If you want to run server outside and just attach to it, you can change "Attach = Yes" in step 5 properties.
Sort of like Xeevis wrote, it was a bit different when I did it though:
project properties, debug page
1) start external program: bin_Win64\PhotonSocketServer.exe
2) command line arguments: /debug Instance1
3) working directory: \bin_Win64\
when using VS2008 make sure to set the CLRVersion to 2.0;
no need to add the exe to the solution.
Instead I had to choose "Add->Existing Project" and now VS allows me to select it as startup project.
I tried to right click my solution and then select add an existing project. But when I try to select the PhotonSocketServer.exe file, my Visual Studio won't allow me to add it. I'm using Microsoft Visual C# 2010 Express.
Are there other ways to debug the Photon Server?
Lite and MMO demo both have a"disconnected" namespace in the test project that shows how to do this.
With photon you will have to use logging.
I don't know of any other way.
I've got some debug information working while logging specifically to a file, so I'm just trying to figure out if there's some other way to get some debug information without always relying on the log file.
Thanks!
I don't think Photon did ever offer a debug window for logging, do you remember where you read about this?
How would that be done with Photon 3? I've tried replacing the existing Runtime declaration in PhotonServer.config with the one you gave - but then, Photon doesn't start up anymore.
In the log, I'm getting:
(the photon host runtime version there is breaking it)
i use VS2010 and .Net 4.0 follow this tutorial but cant debug it...
only use VS2008 and .net2.0 can do this debug mode???
i hope can check Parameter changes at debug and running time.
i should how do it about open like windows or console?
thx a lots ~~
I have changed the CLR version in config file to 2.0 (as stated in the first post) and everything is working for me.
at 2010 i cant attach to process it...
maybe my sysytem have some problem
thx
Which IDE/Debugger are you using? Which OS do you have and is it 32 or 64 bit? Did you configure the runtime for Photon as described above?
My IDE is VS2008 and the OS is WinXP32, It would be i make the bad config, so, i try again.
Thanks Boris!
Is it possible to have clear message in logs (or any other way) which configuration option was violated by client?
For example, my client broke rule MaximumTimeout="3000" which defined in Instance1 section of PhotonServer.config, after this I had disconnected but which reason I did not know from logs and waste much time to understand it.
P S. Note, I still use Photon2, because of this reason viewtopic.php?f=5&t=1256
Trying to debug Photon 3 server code.
Did all like is said above, but the server fails to load my application.
Says that 4.0 Net compiled module cannot be loaded into 2.0 runtime.
And I cant build server with 2.0 cus photon dll reference 3.0 dlls.