PageRenderTime 60ms CodeModel.GetById 56ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/Logger/AccessLogger.cs

http://github.com/jacksonh/manos
C# | 85 lines | 36 code | 15 blank | 34 comment | 3 complexity | 682b0486e6d0d9ce961f72cc85ebae09 MD5 | raw file
 1//
 2// Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
 3//
 4// Permission is hereby granted, free of charge, to any person obtaining
 5// a copy of this software and associated documentation files (the
 6// "Software"), to deal in the Software without restriction, including
 7// without limitation the rights to use, copy, modify, merge, publish,
 8// distribute, sublicense, and/or sell copies of the Software, and to
 9// permit persons to whom the Software is furnished to do so, subject to
10// the following conditions:
11// 
12// The above copyright notice and this permission notice shall be
13// included in all copies or substantial portions of the Software.
14// 
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22//
23//
24
25
26using System;
27using System.IO;
28using System.Text;
29
30using Manos;
31using Manos.Http;
32
33namespace Manos.Util {
34
35	public class AccessLogger : ManosPipe {
36
37		private string path;
38		private FileStream stream;
39
40		public AccessLogger (string path)
41		{
42			if (path == null)
43				throw new ArgumentNullException ("path");
44
45			this.path = path;
46			CreateStream ();
47		}
48
49		public override void OnPostProcessRequest (ManosApp app, IHttpTransaction transaction, Action complete)
50		{
51			// LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
52			// %h - Remote host       -- DONT HAVE
53			// %l - Remote log name   -- DONT HAVE
54			// %u - Remote user       -- DONT HAVE
55			// %t - Date+Time
56			// %r - Request path
57			// %s - Status Code
58			// %b - Bytes sent
59			// %Referer -
60			// %User Agent -
61
62			string line = String.Format ("- - - [{0}] \"{1}\" {2} {3} - -\n",
63					DateTime.Now.ToString ("dd/MMM/yyyy:HH:mm:ss K"),
64					transaction.Request.Path,
65					transaction.Response.StatusCode,
66					transaction.Response.Headers.ContentLength);
67
68			byte [] data = Encoding.Default.GetBytes (line);
69			stream.BeginWrite (data, 0, data.Length, null, null);
70
71			complete ();
72		}
73
74		private void CreateStream ()
75		{
76			string dir = Path.GetDirectoryName (path);
77			// If it exists this does nothing.
78			if (!String.IsNullOrEmpty (dir))
79				Directory.CreateDirectory (dir);
80
81			stream = new FileStream (path, FileMode.Append, FileAccess.Write, FileShare.Read, 8, true);
82		}
83	}
84}
85