I am not sure how you write directly ex (you can try). I think you will have to format or use ex.Message or any other property that you want to save.
To write into a file a simple code as below should do.
TextWriter writer = new StreamWriter(Server.MapPath("~/"LogFileName.txt"));
Since this code is going to run on the server, make sure you have set appropriate access to the folder where you are creating this file.
However, if it logging purposes, I would strongly recommend to use a toolset like Log4Net or any other you are comfortable with. This is because with ASP.Net and multiuser scenario, thing can get ugly very easily.