Monday, April 22, 2013

Shrink Log file SQL Server

After prolong usage of heavy DB transactions and CURD operation MS SQL server DB log file starts getting large. This file is important for the DBA because it determine which recovery method your DB is using but in my case as a developer i don't really have to maintain recovery methods so i rather use to shrink my log file often. Here is the quick snippet to do so so this will help you guys:

USE Northwind
GO
Print 'Set Recovery Mode To Simple'
ALTER DATABASE Northwind SET RECOVERY SIMPLE;
Go
Print 'Set Single User Mode'
ALTER DATABASE Northwind SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Go
print 'Shrink Log File'
Declare @FileName As Varchar(100)
Set @FileName=(SELECT name From sys.database_Files where type=1)
DBCC SHRINKFILE(@FileName, 1)
BACKUP LOG Northwind WITH TRUNCATE_ONLY
DBCC SHRINKFILE(@FileName, 1)
Go
Print 'Set Recovery Back To Full'
ALTER DATABASE Northwind SET RECOVERY FULL;
Go
Print 'Set User Mode To Multi'
ALTER DATABASE Northwind SET MULTI_USER WITH NO_WAIT
Go

Monday, January 7, 2013

A Simple Text File logger for .Net apps

Sometimes you just need a simple kick start while debugging application. Once such scenario is to add simple file based logging. I have created a very simple file based Logger just to add some info to text file and comes handy when you actually want to look at the trace of events specially in a multi-threaded environment that is a little difficult to debug at runtime.

public class Logger
    {
        public static void Log(string str)
        {
            var file = Path.Combine(HttpContext.Current.Server.MapPath(@"~\"), "log.txt");
            System.IO.File.AppendAllText(file, string.Format("{0}-{1}{2}",DateTime.Now, str, Environment.NewLine));
        }
    }