Serilog基于WPF的应用程序工作,但不能作为EXE安装



注意:这是Microsoft.Data.SqlClient及其SqlConnection实现的问题。如果我创建一个实例(_sqlConnection = new SqlConnection();)然后检查状态(_sqlConnection.State)我得到一个NullReferenceException。如果我分配一个有效的连接字符串,仍然是Null异常。

解决方案(为了节省您的时间)是恢复到使用System.Data.SqlClient,一切都很好。我把剩下的留在这里,以防它有助于追查原因-或者如果其他人有类似的问题。

我有一个WPF .net 4.7.2应用程序,当我从VS构建和运行时运行(或在调试或发布中双击.exe)。如果我生成一个。msi文件并从那里安装,我得到一个空异常错误时,写一个日志到MS SQL服务器(但不是到本地文件)

我正在使用:

  • Serilog: 2.10.0
  • Serilog.Sinks。控制台:4.0.0
  • Serilog.Sinks。文件:5.0.0
  • Serilog.Sinks.PeriodicBatching: tripwire
  • Serilog.Sinks。MSSqlServer: 5.6.1
  • MS.Data。SqlClient: 3.0.1
  • System.Data。安装常见:4.3.0
  • System.Data.SqlClient: 4.8.3

.msi文件是这样创建的:MicrosoftVisualStudio InstallerProjects(可从https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2017InstallerProjects&ssr=false#overview获得)

以下代码已被修改,以删除所有干扰以及任何专有信息。请记住,除了作为.msi文件安装之外,这一切都可以正常工作。

当我的应用程序启动时,我调用以下代码:Serilog.Debugging.SelfLog。启用(味精=比;

Debug.WriteLine (msg));导致问题的调用示例:LogDebug("这是一个LogDebug的开发人员测试,由{user}",);

代码:

// Primary method
public void LogDebug(string msgtemplate, params object[] parms){
var LogCfg = GetStdLogConfig();
Log.Logger = LogCfg.CreateLogger();
Log.Warning(msgtemplate, parms);
Log.CloseAndFlush();  // error happens on this line
}  
protected virtual LoggerConfiguration GetStdLogConfig()
{
LoggerConfiguration logcfg = null;
logcfg = new LoggerConfiguration()
.MinimumLevel.ControlledBy(StandardLevelSwitch)
.Enrich.WithProperty("Fid1", AppValues.VersionNo)
.Enrich.WithProperty("Fid2", AppValues.UserId)
.Enrich.WithProperty("Fid3", getUserRightsString())
.Enrich.WithProperty("Fid4", AppValues.AppCompileMode);
AddLocalFile(logcfg, "std"); // this writes to local file just fine.
AddSql(logcfg, "std");
return logcfg;
}
protected virtual LoggerConfiguration AddSql(LoggerConfiguration logcfg, string mode = "")
{
try
{
var logTable = "App_Logs"; // Renamed for sample
var sinkOptions = new MSSqlServerSinkOptions { TableName = logTable };
var columnOptionVals = new ColumnOptions
{
AdditionalColumns = new System.Collections.ObjectModel.Collection<SqlColumn>()
};
columnOptionVals.AdditionalColumns.Add(new SqlColumn("Fid1", System.Data.SqlDbType.NVarChar, true, 50));
columnOptionVals.AdditionalColumns.Add(new SqlColumn("Fid2", System.Data.SqlDbType.NVarChar, true, 15));
columnOptionVals.AdditionalColumns.Add(new SqlColumn("Fid3", System.Data.SqlDbType.NVarChar, true, 100));
columnOptionVals.AdditionalColumns.Add(new SqlColumn("Fid4", System.Data.SqlDbType.NVarChar, true, 20));
columnOptionVals.Store.Remove(StandardColumn.MessageTemplate);
columnOptionVals.LogEvent.DataLength = 2048;
columnOptionVals.PrimaryKey = columnOptionVals.TimeStamp;
columnOptionVals.TimeStamp.NonClusteredIndex = true;

// <Valid ConnectionString> is the same as used to do other updates without issue
logcfg.WriteTo.MSSqlServer(
connectionString: <Valid ConnectionString>,
sinkOptions: sinkOptions,
columnOptions: columnOptionVals
);
return logcfg;
}

错误发生在Log.CloseAndFlush():.

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Microsoft.Data.SqlClient
StackTrace:
at Microsoft.Data.SqlClient.SqlConnection.OpenAsync(CancellationToken cancellationToken)
如果我继续,这个错误显示:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Microsoft.Data.SqlClient
StackTrace:
at Microsoft.Data.SqlClient.SqlConnection.OpenAsync(CancellationToken cancellationToken)
This exception was originally thrown at this call stack:
Microsoft.Data.SqlClient.SqlConnection.OpenAsync(System.Threading.CancellationToken)
System.Data.Common.DbConnection.OpenAsync()
Serilog.Sinks.MSSqlServer.Platform.SqlClient.SqlConnectionWrapper.OpenAsync()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in exceptionservicescommon.cs

如果我在StackTrace中查看这个异常,我注意到额外的信息:

在Microsoft.Data.SqlClient.SqlConnection。OpenAsync (CancellationToken CancellationToken)在System.Data.Common.DbConnection.OpenAsync ()在Serilog.Sinks.MSSqlServer.Platform.SqlClient.SqlConnectionWrapper.d__6.MoveNext ()exception . dispatchinfo . throw () inf: dd 民主党 clr src BCL system runtime exceptionservices exceptionservicescommon.cs: 133行

然后继续(但没有记录到SQL服务器)

在输出窗口中我看到(由于上面的SelfLog调用):

Exception thrown: 'System.NullReferenceException' in mscorlib.dll
Object reference not set to an instance of an object.

当我调试。exe时,在SqlConnection实例化后,许多属性是NullExceptions,而不是NULL。试图给它们赋值没有任何作用,它们仍然是NullExceptions。但是它只发生在这个调用上,SqlConnection的所有其他实例化都按预期工作。

解决方案中的所有其他项目都使用System.Data.SqlClient -所有代码都是这样工作的,只有这个接收器没有。

我根据电子邮件中的打开信息编辑了库。最终修复它(有或没有这些步骤的帮助)是用默认的系统SQL库替换Github MS SQL库。

//using Microsoft.Data.SqlClient; 
using System.Data.SqlClient;

相关内容

最新更新