While there is built in logging using the RequestLogsFeature
, this is aimed at debugging your project.
However ServiceStack does make it easy to get at all the relevant information you need by using a request filter that will be run for each request.
The code below demonstrates how you would record how long a request takes, and the other values you were looking for, such as SessionId, IP address, Verb, URI, etc.
public override void Configure(Funq.Container container)
{
// Record the time the request started.
this.GlobalRequestFilters.Add((req, res, requestDto) => req.SetItem("StartTime", DateTime.Now));
// The request has been processed, ready to send response
this.GlobalResponseFilters.Add((req, res, responseDto) => {
var startTime = req.GetItem("StartTime") as DateTime?;
var endTime = DateTime.Now;
var log = new {
// Request URL
Url = req.AbsoluteUri,
// Verb
Verb = req.Verb,
// Session Id
SessionId = req.GetSessionId(),
// IP Address
Ip = req.RemoteIp,
// Record the Request DTO
Request = req.Dto.ToJson(),
// Record the Response DTO
Response = responseDto.ToJson(),
// Get the start time that was recorded when the request started
StartTime = startTime.Value,
// Time request finished
EndTime = endTime,
// Determine the duration of the request
Duration = endTime - startTime.Value,
};
// Save the log to the database
// Resolve the database connection
var db = TryResolve<IDbConnectionFactory>().OpenDbConnection();
// ...
});
}
I hope this helps.