Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am newly working in WPF project, written in C# language. This application provides the operating base like browser, email, printing, logging etc to other applications on workstations.

Now a particular application wants to trace the HTTPRequests and HTTPResponses and want us to log them into log files.

Can anyone suggest what and how to do that?
Posted

1 solution

If does not matter what you log and is it WPF or not. One possibility is using the class System.Diagnostics.EventLog:
https://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28v=vs.110%29.aspx[^].

One popular open-source 3rd-party product you can use is Apache Log4net:
http://en.wikipedia.org/wiki/Log4j#Ports[^],
http://logging.apache.org/log4net[^],
https://github.com/apache/log4net[^].

The points of code where you can grab HTTP request/response data depends on what you are doing.

—SA
 
Share this answer
 
Comments
Member 10145129 26-Feb-15 10:39am    
Thank Sergey for the links.
I want to grap the HTTPResponse/Requests at the point of printing the application does through this base application.
Now my question is, is there any additional component required to grab the HTTPrequest/responses? Or else how to do that? Or if Log4Net is enabled then that will log it as a whole?
Sergey Alexandrovich Kryukov 26-Feb-15 11:06am    
It depends on what you are doing. You did not share relevant detail. If you already provided "like browsing" and other operations, you already send and receive HTTP request, so why any more components? If you did not do it yet, you have to address that problem first. :-)
—SA
Member 10145129 27-Feb-15 8:47am    
Hi Sergey,

In below code I am Logging the http call details
a. URL address
b. Response code
c. Size of package
d. Call duration

Now I want to add iterative requests if more files are included and need to be downloaded.Can You please help?


Expand | Copy Code
Public Function NavigateTo(uriString As String) As Response
Dim resp As New Response
Dim request As HttpWebRequest = CType(WebRequest.Create(uriString), HttpWebRequest)
Dim startTime, endTime As Date

request.MaximumAutomaticRedirections = 4
request.MaximumResponseHeadersLength = 4

request.Credentials = CredentialCache.DefaultCredentials

startTime = Date.Now()

Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

endTime = Date.Now()

Dim receiveStream As Stream = response.GetResponseStream()

Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)

resp.StrResponse = readStream.ReadToEnd()
resp.ResponseCode = Integer.Parse(response.StatusCode).ToString()
resp.Size = response.ContentLength.ToString()
resp.Time = endTime.Subtract(startTime).TotalMilliseconds.ToString()

response.Close()
readStream.Close()

Return resp
Sergey Alexandrovich Kryukov 27-Feb-15 8:52am    
All right, very good, log whatever you want.
—SA
Member 10145129 27-Feb-15 8:55am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900