如何在多个类中使用一个与serilog一起使用一个logfile
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File("logs/ConverterLog.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
Log.Information("Program Startup");
}
}
我正在尝试将Serilog实现到我的Windows表单应用程序中,但我不知道如何登录多个类以写入同一文件。例如,当我使用log.formertion时,例如在类SQLREAD中,它不会将其添加到类程序中配置的logFile中。
class SqlRead
{
public void Password(string sqlConnectionString, List<SqlInformation> sqlInformationList)
{
string sqlQuerySelect = "SELECT Login, Password FROM Users Order by Login";
using var sqlConn = new SqlConnection(sqlConnectionString);
using var sqlCmdSelect = new SqlCommand(sqlQuerySelect, sqlConn);
try
{
sqlConn.Open();
using SqlDataReader sqlReader = sqlCmdSelect.ExecuteReader();
while (sqlReader.Read())
{
sqlInformationList.Add(new SqlInformation() { Username = sqlReader[0].ToString(), Password = sqlReader[1].ToString() });
}
sqlReader.Close();
Log.Information("SQL data successfully read");
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
Log.Error(e.Message);
}
finally
{
Log.CloseAndFlush();
}
}
}
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File("logs/ConverterLog.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
Log.Information("Program Startup");
}
}
I am trying to implement Serilog into my Windows Forms application but I can't figure out how to log over multiple classes to write into the same file. When I use Log.Information for example in the class SqlRead, it does not get added to logfile which is configured in the class Program.
class SqlRead
{
public void Password(string sqlConnectionString, List<SqlInformation> sqlInformationList)
{
string sqlQuerySelect = "SELECT Login, Password FROM Users Order by Login";
using var sqlConn = new SqlConnection(sqlConnectionString);
using var sqlCmdSelect = new SqlCommand(sqlQuerySelect, sqlConn);
try
{
sqlConn.Open();
using SqlDataReader sqlReader = sqlCmdSelect.ExecuteReader();
while (sqlReader.Read())
{
sqlInformationList.Add(new SqlInformation() { Username = sqlReader[0].ToString(), Password = sqlReader[1].ToString() });
}
sqlReader.Close();
Log.Information("SQL data successfully read");
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
Log.Error(e.Message);
}
finally
{
Log.CloseAndFlush();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的原始代码(带有我的评论):
我认为应该有效:
在所有其他类中删除任何
colleandflush()
调用。Your original code (with comments from me):
What I think should work:
Remove any
CloseAndFlush()
calls in all other classes.