I am new to C#, so please bear with me as I have inherited a script that I'm attempting to tweak.
I want to get the output of SQL PRINT/RAISERROR statements to show up in a log file that has been declared in another part of the script.
This is my method I'm calling:
public void ProcessData(string StoredProcedure, int StartDate, int EndDate, string Directory, string LogFileNameAndPath)
{
SqlConnection sqlConnection = null;
SqlCommand sqlCommand = null;
SqlParameter sqlParameter = null;
// String outputText = null;
try
{
sqlConnection = new SqlConnection(_ConnectionString);
sqlConnection.Open();
sqlCommand = new SqlCommand();
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandText = StoredProcedure;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandTimeout = 0;
sqlParameter = new SqlParameter("@StartDt", SqlDbType.Int);
sqlParameter.Value = StartDate;
sqlCommand.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@EndDt", SqlDbType.Int);
sqlParameter.Value = EndDate;
sqlCommand.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("@stringDirs", SqlDbType.VarChar);
sqlParameter.Value = Directory;
sqlCommand.Parameters.Add(sqlParameter);
sqlConnection.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);
sqlCommand.ExecuteNonQuery();
}
catch (SqlException sqlEx)
{
throw sqlEx;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
if (sqlConnection != null)
{
if (sqlConnection.State != ConnectionState.Closed)
{
sqlConnection.Close();
}
}
}
}
This is the info handler method:
public void OnInfoMessage(object sender, SqlInfoMessageEventArgs args)//, String LogFileNameAndPath)
{
foreach (SqlError err in args.Errors)
{
//File.AppendAllText(LogFileNameAndPath, err.Message);
Console.WriteLine("{0}", err.Message);
//return err.Message;
//"The {0} has received a severity {1}, state {2} error number {3}\n" +
// "on line {4} of procedure {5} on server {6}:\n{7}",
// err.Source, err.Class, err.State, err.Number, err.LineNumber,
// err.Procedure, err.Server, err.Message);
}
}
Instead of outputting to the Console, I want to write it to the LogFileNameAndPath variable via the "File.AppendAllText(LogFileNameAndPath, err.Message)"; however, I have looked at many posts over the web and NOBODY provides a solution. Is there a way to do this? Please be kind. Thanks!
===========================================================================
[ADDED 2015-07-27 1606 EDT] If I change this line from:
sqlConnection.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);
... to ...
sqlConnection.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage(LogFileNameAndPath));
... it fails to compile. How does this get passed to the method?
Here's the new method:
public void OnInfoMessage(object sender, SqlInfoMessageEventArgs args, String LogFileNameAndPath)
{
foreach (SqlError err in args.Errors)
{
File.AppendAllText(@LogFileNameAndPath, err.Message);
//Console.WriteLine("{0}", err.Message);
}
}
File.AppendAllText()call doesn't work? Are you receiving a specific error?