Friday, July 1, 2011

Custom Logger in ASP.NET along with access permission to folder

This class can be used to create custom logger in ASP.NET application which includes the functionality of giving the permissions to the log folder where seperate log file will be created for each day.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.AccessControl;

namespace MyNamespace.Logger
{
    public class Logger
    {       
        public static void CreateLog(string strMessage, 
string moduleName, bool isError)
        {
            try
            {
                string strIsError = string.Empty;
                if (isError)
                    strIsError = "Error:";
                else
                    strIsError = "Information:";

                string fileUrl = GetFileUrl();
                string directoryName = System.IO.Path.GetDirectoryName(fileUrl);
                CreateFolder(directoryName);

                using (System.IO.StreamWriter sw = System.IO.File.AppendText(fileUrl))
                {
                    if (string.IsNullOrEmpty(moduleName) == false)
                        moduleName = "(" + moduleName + ")";

                    sw.WriteLine(strIsError + strMessage + "\t" + 
DateTime.Now.ToString() + "\t" + moduleName);
                }
            }
            catch (Exception)
            {
        throw ex;
            }
        }

        public static void Error(string message, Exception ex)
        {
            CreateLog(message + "\nStacktrace: " + ex.StackTrace, string.Empty, true);
        }

        public static void Info(string message, Exception ex)
        {
            CreateLog(message + "\nStacktrace: " + ex.StackTrace, string.Empty, false);
        }
       
        public static string GetFileUrl()
        {
            AppConfig objAppConfig = new AppConfig();
            string strPath = objAppConfig.LogFilePath;
            string strFileName = strPath.Substring(strPath.LastIndexOf('\\') + 1);
            if (!strFileName.EndsWith(".txt") || string.IsNullOrEmpty(strFileName))
            {
                strFileName = "Logs.txt";
                if (strPath.EndsWith("\\") == false)
                    strPath += "\\";
            }
            strFileName = GetFileNameFormat() + strFileName;

            return System.IO.Path.Combine(System.IO.Path.GetDirectoryName(strPath), strFileName);
        }

        private static string GetFileNameFormat()
        {
            string format = string.Empty;

            string month = DateTime.Now.Month.ToString();
            string day = DateTime.Now.Day.ToString();

            if (month.Length < 2)
                month = "0" + month;

            if (day.Length < 2)
                day = "0" + day;

            return DateTime.Now.Year.ToString() + "_" + month + "_" + day + "_";
        }

        private static bool CreateFolder(string folderPath)
        {
            try
            {
                SetAccessRightsToFolder(folderPath);
                Directory.CreateDirectory(folderPath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static void SetAccessRightsToFolder(string folderPath)
        {
            try
            {
                string machineName = System.Environment.MachineName;

                AddAccessRuleForFolder(folderPath, "Everyone");
                AddAccessRuleForFolder(folderPath, machineName + "\\Everyone");

                AddAccessRuleForFolder(folderPath, "ASPNET");
                AddAccessRuleForFolder(folderPath, machineName + "\\ASPNET");

                // NOTE: Even "NETWORK SERVICE" would be fine without "NT AUTHORITY\".
                AddAccessRuleForFolder(folderPath, "NETWORK SERVICE");
                AddAccessRuleForFolder(folderPath, machineName + "\\NETWORK SERVICE");
                AddAccessRuleForFolder(folderPath, "NT AUTHORITY\\NETWORK SERVICE");
            }
            catch (Exception)
            {
        throw ex;
            }
        }

        private static void AddAccessRuleForFolder(string folderPath, 
string groupOrUserName)
        {
            try
            {
                DirectoryInfo folderDirInfo = new DirectoryInfo(folderPath);
                DirectorySecurity dirSecurity = folderDirInfo.GetAccessControl();

                //Access Rights applies to Folder and SubFolders 
                dirSecurity.AddAccessRule(new FileSystemAccessRule
(groupOrUserName, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, 
PropagationFlags.None, AccessControlType.Allow));
                //Access Rights applies to files inside the folder
                dirSecurity.AddAccessRule(new FileSystemAccessRule
(groupOrUserName, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, 
PropagationFlags.None, AccessControlType.Allow));

                folderDirInfo.SetAccessControl(dirSecurity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

Select and Delete text vertically in bigger text OR flat file

If you want to Select and Delete text vertically in bigger text OR any flat file use below command to do it. Example below: A BCEDF CDEF...