我是C#的新手,所以请耐心等待,因为我继承了一个我试图调整的脚本.
我想让SQL PRINT/RAISERROR语句的输出显示在已在脚本的另一部分中声明的日志文件中.
这是我打电话的方法:
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是信息处理程序方法:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想通过"File.AppendAllText(LogFileNameAndPath,err.Message)"将其写入LogFileNameAndPath变量,而不是输出到控制台.但是,我通过网络查看了很多帖子,NOBODY提供了解决方案.有没有办法做到这一点?请善待.谢谢!
================================================== =========================
[ADDED 2015-07-27 1606 EDT]如果我更改此行:
sqlConnection.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);
Run Code Online (Sandbox Code Playgroud)
... 至 ...
sqlConnection.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage(LogFileNameAndPath));
Run Code Online (Sandbox Code Playgroud)
......它无法编译. 这如何传递给方法?
这是新方法:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
您不应尝试将日志文件名传递到事件处理程序中,而应在其他位置定义日志文件名,并在事件处理程序内可见。例如:
public class SqlHandler
{
private readonly string m_LogFileNameAndPath = @"C:\log.txt";
// or get it from AppConfig, as noted by FirebladeDan
public void ProcessData() { /*...*/ }
public void OnInfoMessage(object sender, SqlInfoMessageEventArgs args)
{
foreach(var err in args.Errors)
{
File.AppendAllText(m_LogFileNameAndPath, err.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
还值得注意的是,您仍然可以通过告诉库要记录到哪个文件来将日志记录库(Log4Net、NLog 等)与自定义文件一起使用。
| 归档时间: |
|
| 查看次数: |
559 次 |
| 最近记录: |