September 2010

Implement Logger in asp.net

First of create LoggingConstant.cs File in that Declare all Pages name as variable without extension.
For ex.
public enum ModuleToLog
{
default,
login
}
public enum LogType
{
DEBUG,
EXCEPTION,
INFO
}

After that create Logger.cs File..

public class Logger
{
public static void Write(LogType eLogType, ModuleToLog eModule, string strMethodName, string LogMessage, string strUserId)
{

string LogFilePath = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath + “/Logs”);

//Create directory for Year/Month/
LogFilePath = string.Format(“{0}/{1}/{2}”, LogFilePath, DateTime.Now.Year.ToString(), DateTime.Now.ToString(“MMM”));
DirectoryInfo ObjDirectoryInfo = new DirectoryInfo(LogFilePath);
//string strLogFile = string.Format(“{0}/{1}.log”, LogFilePath, DateTime.Now.ToString(“dd-MMM-yyyy”));

string strLogFile = string.Format(“{0}/{1}.log”, LogFilePath, strUserId + “_” + DateTime.Now.ToString(“ddMMMyyyy”));
try
{
if (ObjDirectoryInfo.Exists == false)
{
ObjDirectoryInfo.Create();
}

StreamWriter sw = new StreamWriter(strLogFile, true);

sw.WriteLine(string.Format(“[{0}][{1}][{2}][{3}][{4}] {5}”, DateTime.Now.ToString(), eLogType.ToString(), eModule.ToString(), strMethodName, strUserId, LogMessage));
sw.Close();
sw.Dispose();
}
catch (Exception ex)
{

}
}
}

——–call this function —
try
{
Logger.Write(LogType.INFO, ModuleToLog.Default, “btnSave_Click”, “Write Comments”, loggedinUserName);

}
catch (Exception ex)
{
Logger.Write(LogType.EXCEPTION, ModuleToLog.WriteBoardBookComments, “btnSave_Click”, ex.StackTrace.ToString() , userdetail.Name);
}

Windows Users List in Asp.Net c#

using System.Management;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SelectQuery query = new SelectQuery(“Win32_UserAccount”);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject envVar in searcher.Get())
{
Console.WriteLine(“Username : {0}”, envVar[“Name”]);
}

Console.ReadLine();

}
}
}

create xml file dynamically

try
{
if (System.Web.HttpContext.Current.Session[“UserId”] != null && System.Web.HttpContext.Current.Session[“EventId”] != null && System.Web.HttpContext.Current.Session[“EventConfiguration”] != null)
{

Logger.Write(LogType.INFO, ModuleToLog.EventInformation, “WriteEventHistory”, “EventHistory”, HttpContext.Current.Session[“UserName”].ToString());
string pageName = System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.ServerVariables[“SCRIPT_NAME”]);
UserService.UserClient proxyFind = new ePlannerPro2Client.UserService.UserClient();

epp2ClientPL.UsersProperties result = proxyFind.LoadById(new Guid(Convert.ToString(System.Web.HttpContext.Current.Session[“UserId”])), new Guid(Convert.ToString(System.Web.HttpContext.Current.Session[“EventId”])));

if (result == null)
{
return;
}
//epp2ClientPL.UsersProperties result = proxyFind.LoadUsersByEventId(new Guid(Convert.ToString(Convert.ToString(System.Web.HttpContext.Current.Session[“EventId”])))).Where(u => u.UserId.Equals(new Guid(Convert.ToString(System.Web.HttpContext.Current.Session[“UserId”])))).SingleOrDefault();

epp2ClientPL.EventConfigurationProperties files = (epp2ClientPL.EventConfigurationProperties)System.Web.HttpContext.Current.Session[“EventConfiguration”];

string FilePath = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath + “/XML”);
DirectoryInfo ObjDirectoryInfo = new DirectoryInfo(FilePath);

string strFileName = FilePath + “/” + files.EventTitle.ToString() + “_History.xml”;

XmlDocument XmlFile = new XmlDocument();

if (ObjDirectoryInfo.Exists == false)
{
ObjDirectoryInfo.Create();
}

FileInfo fi = new FileInfo(strFileName);

if (fi.Exists)
{
XmlFile.Load(strFileName);
}
else
{
XmlNode Declaration = XmlFile.CreateNode(XmlNodeType.XmlDeclaration, “”, “”);
XmlFile.AppendChild(Declaration);
XmlElement OperationElement;
OperationElement = XmlFile.CreateElement(“Operations”);
XmlFile.AppendChild(OperationElement);
XmlFile.Save(strFileName);
}

XmlNode Operations = XmlFile.SelectSingleNode(“Operations”);

XmlNode Operation1;
Operation1 = XmlFile.CreateNode(XmlNodeType.Element, “Operation”, “”);
Operations.AppendChild(Operation1);

XmlNode EventAttendeeId;
EventAttendeeId = XmlFile.CreateNode(XmlNodeType.Element, “EventAttendeeId”, “”);
EventAttendeeId.InnerText = Convert.ToString(result.EventAttendeeId);
Operation1.AppendChild(EventAttendeeId);

XmlNode FullName;
FullName = XmlFile.CreateNode(XmlNodeType.Element, “FullName”, “”);
FullName.InnerText = result.FullName;
Operation1.AppendChild(FullName);

MenusService.MenusClient proxyPage = new ePlannerPro2Client.MenusService.MenusClient();

#region “Get Page Id”
ePlannerPro2Client.MenusService.MenusClient proxyMenus = new ePlannerPro2Client.MenusService.MenusClient();
epp2ClientPL.MenusProperties[] data = proxyMenus.LoadPageMasterNameId(pageName).ToArray();
if (data == null)
return;
if (data.Count() == 0)
return;
#endregion

XmlNode PageId;
PageId = XmlFile.CreateNode(XmlNodeType.Element, “PageId”, “”);
PageId.InnerText = data[0].Id.ToString();
Operation1.AppendChild(PageId);

XmlNode OT;
OT = XmlFile.CreateNode(XmlNodeType.Element, “OperationType”, “”);
OT.InnerText = OperationType;
Operation1.AppendChild(OT);

XmlNode OperationMessage;
OperationMessage = XmlFile.CreateNode(XmlNodeType.Element, “OperationMessage”, “”);

// string strOperation = result.FullName + ” ” + Operation;

string strOperation = Operation;
OperationMessage.InnerText = strOperation;
Operation1.AppendChild(OperationMessage);

#region “Get Time Zone Name”
ePlannerPro2Client.TimeZoneService.TimeZone timeZone = LoadTimeZonesById(new Guid(files.TimeZoneId.ToString()));
#endregion

XmlNode OperationDate;
OperationDate = XmlFile.CreateNode(XmlNodeType.Element, “OperationDate”, “”);
// OperationDate.InnerText = DateTime.Now.ToString(“MMM dd yyyy, hh:mm:ss tt”);
OperationDate.InnerText = DateTime.Now.ToString(“MM/dd/yyyy, hh:mm:ss tt”);
Operation1.AppendChild(OperationDate);

XmlFile.Save(strFileName);
}
}
catch (Exception ex)
{
Logger.Write(LogType.EXCEPTION, ModuleToLog.EventInformation, “WriteEventHistory”, ex.StackTrace, HttpContext.Current.Session[“UserName”].ToString());
}