/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. using System;
  25. using System.IO;
  26. using System.Text;
  27. using Manos;
  28. using Manos.Http;
  29. namespace Manos.Util {
  30. public class AccessLogger : ManosPipe {
  31. private string path;
  32. private FileStream stream;
  33. public AccessLogger (string path)
  34. {
  35. if (path == null)
  36. throw new ArgumentNullException ("path");
  37. this.path = path;
  38. CreateStream ();
  39. }
  40. public override void OnPostProcessRequest (ManosApp app, IHttpTransaction transaction, Action complete)
  41. {
  42. // LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
  43. // %h - Remote host -- DONT HAVE
  44. // %l - Remote log name -- DONT HAVE
  45. // %u - Remote user -- DONT HAVE
  46. // %t - Date+Time
  47. // %r - Request path
  48. // %s - Status Code
  49. // %b - Bytes sent
  50. // %Referer -
  51. // %User Agent -
  52. string line = String.Format ("- - - [{0}] \"{1}\" {2} {3} - -\n",
  53. DateTime.Now.ToString ("dd/MMM/yyyy:HH:mm:ss K"),
  54. transaction.Request.Path,
  55. transaction.Response.StatusCode,
  56. transaction.Response.Headers.ContentLength);
  57. byte [] data = Encoding.Default.GetBytes (line);
  58. stream.BeginWrite (data, 0, data.Length, null, null);
  59. complete ();
  60. }
  61. private void CreateStream ()
  62. {
  63. string dir = Path.GetDirectoryName (path);
  64. // If it exists this does nothing.
  65. if (!String.IsNullOrEmpty (dir))
  66. Directory.CreateDirectory (dir);
  67. stream = new FileStream (path, FileMode.Append, FileAccess.Write, FileShare.Read, 8, true);
  68. }
  69. }
  70. }