Friday, December 6, 2013

How to use SQLCacheDependency in ASP.NET MVC

Database
Database : Employee
Table : EmpDetails
Developer Command Prompt
aspnet_regsql.exe -S DBServerName -E -d database name -ed 
aspnet_regsql.exe -S DBServerName -E -d database name -t table name -et
Web.Config
  
    
      
        
          
        
      
    
  
Cache Class
public class SQLDependencyCacheProvider
    {
        private static System.Web.Caching.Cache _cache;

        public System.Web.Caching.Cache Cache
        {
            get
            {
                if (_cache == null)
                {
                    _cache = HttpRuntime.Cache;
                }

                return _cache;
            }
        }

        public T GetCacheItem(string key)
        {
            var item = Cache.Get(key);
            return (item != null) ? (T)Convert.ChangeType(item, typeof(T)) : default(T);
        }

        public void InsertCacheItem(string key, T value)
        {
            Cache.Insert(key, value, new SqlCacheDependency("Employee", "EmpDetails"));
        }
    }


Thursday, December 5, 2013

How to Configure ASP.NET Tracing for ASP.NET MVC Applications

  1. ASP.NET Application Level Tracing
  2. Writting Custom ASP.NET Trace Messages using System.Diagnostics.TraceSource class and trace sources
  3. Writting Custom ASP.NET Trace Messages using System.Diagnostics.Trace class
PRE-Requisite(Trace Complier Constant) : Trace statements (Custom tracing) require the TRACE compiler constant, but it’s on by default and you can verify that in the Build tab of the project properties window
The problem is that this setting in the .csproj file only applies to .cs files. ASP.NET uses a different compile process for .cshtml files (or .aspx files in Web Forms), and the settings for that compile process are in the Web.config file. If you don’t explicitly specify the TRACE constant there, tracing method calls in .cshtml views are ignored. Below is an example of what you have to add to the application Web.config file for a Visual Studio project that targets .NET 4.5, in order to define a TRACE constant for the .cshtml compiler:
 
   
     
     
   
 

ASP.NET Application Level Tracing

Web.Config entries

  
    
  


    
       
      
      
      
    
    
        
         
          
          
          
                   
         
       
      
        
          
          
          
        
          
    
    
      
      
    
    
      
        
        
      
    
  
C# Code
 using System.Diagnostics;
 public class AccountController : Controller
    {
        public ActionResult Index()
        {            
            TraceSource source = new TraceSource("AccountController");
            source.TraceEvent(TraceEventType.Warning, 100, "This is message logged from Account Controller");
            return View();
        }

    }
Writting Custom ASP.NET Trace Messages using System.Diagnostics.Trace class
 using System.Diagnostics;
 public class AccountController : Controller
    {
        public ActionResult Index()
        {          
            Trace.WriteLine("This is message logged from Account Controller");
            return View();
        }

    }