TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Write Log Error File in ASP.NET
Pintoo Yadav
Aug 13
2015
Code
1.8
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.IO;
using
System.Web.Services.Description;
using
System.Configuration;
namespace
AdCTS_Web.Controller {
public
class
WriteLogFile {
public
void
AddLog(String vMsg) {
File.AppendAllText(ErrorLogPath(), Environment.NewLine);
File.AppendAllText(ErrorLogPath(), vMsg);
}
private
String ErrorLogPath() {
string
mErrorLogPath =
string
.Empty;
if
(!Directory.Exists(mErrorLogPath)) {
Directory.CreateDirectory(mErrorLogPath);
}
mErrorLogPath = mErrorLogPath + DateTime.Now.ToString(
"dd-MMM-yyyy HH"
) +
".txt"
;
return
mErrorLogPath;
}
public
static
void
Write_LogFile(
string
msg) {
string
mLogPath = HttpContext.Current.Server.MapPath(@
"\\Logs"
);
string
mErrorLogPath =
string
.Empty;
if
(!Directory.Exists(mLogPath)) {
{
mLogPath = mLogPath +
"\\" + DateTime.Now.ToString("
dd-MMM-yyyy-HH");
if
(!Directory.Exists(mLogPath)) {
Directory.CreateDirectory(mLogPath);
}
FileStream fs =
new
FileStream(mLogPath +
"\\" + DateTime.Now.ToString("
dd_MM_yyyy
") + "
_error.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw =
new
StreamWriter(fs);
sw.WriteLine(
"\n\n\t"
);
sw.WriteLine(DateTime.Now.ToString(
"dd_MM_yyyy h:mm tt"
));
sw.Write(
"\t Error :"
+ msg);
sw.Close();
fs.Close();
}
}
else
{
mLogPath = mLogPath +
"\\" + DateTime.Now.ToString("
dd-MMM-yyyy-HH");
if
(!Directory.Exists(mLogPath)) {
Directory.CreateDirectory(mLogPath);
}
FileStream fs =
new
FileStream(mLogPath +
"\\" + DateTime.Now.ToString("
dd_MM_yyyy
") + "
_error.txt", FileMode.Append, FileAccess.Write);
// FileStream fs = new FileStream("Logs\\" + DateTime.Now.ToString("dd_MM_yyyy") + "_error.txt", FileMode.CreateNew, FileAccess.Write);
StreamWriter sw =
new
StreamWriter(fs);
sw.WriteLine(
"\n\n\t"
);
sw.WriteLine(DateTime.Now.ToString(
"dd_MM_yyyy h:mm tt"
));
sw.Write(
"\t Error :"
+ msg);
sw.Close();
fs.Close();
}
}
}
}
log error file
asp.net