Serializing HttpContext object to store in a database

∥☆過路亽.° 提交于 2019-12-14 02:43:42

问题


We have a C# ASP.Net MVC website. I would like to store everything contained in the HttpContext.Current.Request object in a table in SQL Server for later analysis.

I attempted serializing the object into a Byte Array, then storing it in a binary field in SQL Server, however the serializer won't serialize the HttpContext object since it is not marked Serializable.

Is there any way to mark a System.Web class as serializable? Or is there another way to accomplish this?


回答1:


For production code, I don't believe you can change the serialization aspect of the HttpContext. If this is non-production you might try modifying the Assembly to insert the Serializable attribute (but that may need to be done on sub-classes too) using a tool like Reflector and some plug-ins.

What might be an easier option would be to use Reflection to enumerate all the public and private fields. You could convert simple types to their string representation and recurse on classes that are slightly more complicated. You might even be able to find a tool to do this, the idea would probably be referred to ask an Object Graph viewer.




回答2:


I'd advise against storing the entire request, there would be far too much noise.

Instead pick out the signal that you want. That is log specifically what you are interested in, e.g. particular variables.



来源:https://stackoverflow.com/questions/12858064/serializing-httpcontext-object-to-store-in-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!