PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSASPNETMVCCustomActionFilter/ActionFilters/TextLogActionFilter.cs

#
C# | 68 lines | 38 code | 8 blank | 22 comment | 0 complexity | 0521018307c2832abd3cdb3bab08cf5a MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: MessageModifierActionFilter.cs
  3. * Project: CSASPNETMVCCustomActionFilter
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * The CSASPNETMVCCustomActionFilter sample demonstrates how to use C# codes to
  7. * create custom ActionFilters for ASP.NET MVC web application. In this sample,
  8. * there are two custom ActionFilters, one is used for customizing ViewData
  9. * before page view result get executed and rendered; another is used for
  10. * perform logging within the various events during the processing of custom
  11. * ActionFilters.
  12. *
  13. *
  14. * This source is subject to the Microsoft Public License.
  15. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  16. * All other rights reserved.
  17. *
  18. * History:
  19. * * 10/10/2009 1:35 PM Steven Cheng Created
  20. ***************************************************************************/
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Text;
  25. using System.Web.Mvc;
  26. using System.IO;
  27. namespace CSASPNETMVCCustomActionFilter.ActionFilters
  28. {
  29. public class TextLogActionFilter : ActionFilterAttribute
  30. {
  31. // Property for specifying log file path
  32. public string LogFileName { get; set; }
  33. // Default constructor
  34. public TextLogActionFilter() { LogFileName = "MVC_ACTION_FILTER_LOG.TXT"; }
  35. public override void OnActionExecuted(ActionExecutedContext filterContext)
  36. {
  37. StreamWriter sw = File.AppendText(LogFileName);
  38. sw.WriteLine(">>>TextLogActionFilter.OnActionExecuted, {0}", DateTime.Now);
  39. sw.Close();
  40. }
  41. public override void OnActionExecuting(ActionExecutingContext filterContext)
  42. {
  43. StreamWriter sw = File.AppendText(LogFileName);
  44. sw.WriteLine(">>>TextLogActionFilter.OnActionExecuting, {0}", DateTime.Now);
  45. sw.Close();
  46. }
  47. public override void OnResultExecuted(ResultExecutedContext filterContext)
  48. {
  49. StreamWriter sw = File.AppendText(LogFileName);
  50. sw.WriteLine(">>>TextLogActionFilter.OnResultExecuted, {0}", DateTime.Now);
  51. sw.Close();
  52. }
  53. public override void OnResultExecuting(ResultExecutingContext filterContext)
  54. {
  55. StreamWriter sw = File.AppendText(LogFileName);
  56. sw.WriteLine(">>>TextLogActionFilter.OnResultExecuting, {0}", DateTime.Now);
  57. sw.Close();
  58. }
  59. }
  60. }