How to log warning and Informational message ?

  • Thread starter Thread starter Jparv
  • Start date Start date
J

Jparv

Guest
Hello,

Whenever error comes it logs the error from catch block,but dont know how to log the error from try block and in else part because its giving me error in oErrorLog.WriteInformationLog(ex, e);

private void btnFieldMapping_Click(object sender, EventArgs e)
{
if (FieldMapping.Rows.Count > 0)
{
try
{
if (fieldMappingHelperObj.CreateExcelFile(FilePath.Text, JObdetails))
{
MessageBox.Show("Completed Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
//oErrorLog.WriteInformationLog(ex, e);

}
}
catch (Exception ex)
{
MessageBox.Show("Field Mapping Creation failed", "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
oErrorLog.WriteErrorLog(ex, e);
}
}
else
{
MessageBox.Show("Please read JOB file and then proceed", "Invalid file type", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
ErrorLog Function

public bool WriteErrorLog(Exception LogMessage,Object Obj)
{
bool Status = false;
//string LogDirectory = ConfigurationManager.AppSettings["LogDirectory"];
string LogDirectory = System.Configuration.ConfigurationManager.AppSettings["LogDirectory"];

DateTime CurrentDateTime = DateTime.Now;
string CurrentDateTimeString = CurrentDateTime.ToString();
CheckCreateLogDirectory(LogDirectory);
string logLine = BuildLogLine(CurrentDateTime, LogMessage.StackTrace);
LogDirectory = (LogDirectory + "Error_Log" + LogFileName(DateTime.Now) + ".log");

lock (typeof(ErrorLog))
{
StreamWriter oStreamWriter = null;
try
{
oStreamWriter = new StreamWriter(LogDirectory, true);
oStreamWriter.WriteLine(logLine);
Status = true;
}
catch
{

}
finally
{
if (oStreamWriter != null)
{
oStreamWriter.Close();
}
}
}
return Status;
}

public bool WriteInformationLog(Exception LogMessage, Object Obj)
{
bool Status = false;
// string LogDirectory = .["LogDirectory"].ToString();
//string LogDirectory = System.ConfigurationManager.AppSettings["LogDirectory"];
string LogDirectory = ConfigurationManager.AppSettings["LogDirectory"].ToString();
DateTime CurrentDateTime = DateTime.Now;
string CurrentDateTimeString = CurrentDateTime.ToString();
CheckCreateLogDirectory(LogDirectory);
string logLine = BuildLogLine(CurrentDateTime, LogMessage.StackTrace);
LogDirectory = (LogDirectory + "InformationLog_" + LogFileName(DateTime.Now) + ".log");

lock (typeof(ErrorLog))
{
StreamWriter oStreamWriter = null;
try
{
oStreamWriter = new StreamWriter(LogDirectory, true);
oStreamWriter.WriteLine(logLine);
Status = true;
}
catch
{

}
finally
{
if (oStreamWriter != null)
{
oStreamWriter.Close();
}
}
}
return Status;
}



Thanks,

Continue reading...
 
Back
Top