PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Web.Mvc;
27using System.IO;
28
29namespace CSASPNETMVCCustomActionFilter.ActionFilters
30{
31    public class TextLogActionFilter : ActionFilterAttribute
32    {
33        // Property for specifying log file path
34        public string LogFileName { get; set; }
35
36        // Default constructor
37        public TextLogActionFilter() { LogFileName = "MVC_ACTION_FILTER_LOG.TXT"; }
38
39        public override void OnActionExecuted(ActionExecutedContext filterContext)
40        {
41            StreamWriter sw = File.AppendText(LogFileName);
42            sw.WriteLine(">>>TextLogActionFilter.OnActionExecuted, {0}", DateTime.Now);
43            sw.Close(); 
44        }
45
46        public override void OnActionExecuting(ActionExecutingContext filterContext)
47        {
48            StreamWriter sw = File.AppendText(LogFileName);
49            sw.WriteLine(">>>TextLogActionFilter.OnActionExecuting, {0}", DateTime.Now);
50            sw.Close();
51        }
52
53        public override void OnResultExecuted(ResultExecutedContext filterContext)
54        {
55            StreamWriter sw = File.AppendText(LogFileName);
56            sw.WriteLine(">>>TextLogActionFilter.OnResultExecuted, {0}", DateTime.Now);
57            sw.Close();
58        }
59
60        public override void OnResultExecuting(ResultExecutingContext filterContext)
61        { 
62            StreamWriter sw = File.AppendText(LogFileName);
63            sw.WriteLine(">>>TextLogActionFilter.OnResultExecuting, {0}", DateTime.Now);
64            sw.Close();
65        }
66
67    }
68}