C# NLog异步目标数据库连接异常-如何获取它们?

C# NLog异步目标数据库连接异常-如何获取它们?,c#,asynchronous,azure-web-app-service,nlog,error-logging,C#,Asynchronous,Azure Web App Service,Nlog,Error Logging,如果NLog正在使用AsyncTargetTrapper登录数据库,并且存在连接问题,有人能帮我解决这个问题吗?如何获取/截获这些异常(因为异步目标不抛出异常)?这似乎是一种非常常见的情况,因此我很难相信InternalLogger是唯一的选择。下面是一个示例项目(需要安装NLog nuget软件包): 因此应用程序无法连接到服务器(连接字符串中的服务器名称无效)。在达到某个默认(隐藏在NLog的内脏中?)连接超时后,通过InternalLogger将异常打印到控制台: 2016-01-20 1

如果NLog正在使用
AsyncTargetTrapper
登录数据库,并且存在连接问题,有人能帮我解决这个问题吗?如何获取/截获这些异常(因为异步目标不抛出异常)?这似乎是一种非常常见的情况,因此我很难相信
InternalLogger
是唯一的选择。下面是一个示例项目(需要安装NLog nuget软件包):

因此应用程序无法连接到服务器(连接字符串中的服务器名称无效)。在达到某个默认(隐藏在NLog的内脏中?)连接超时后,通过
InternalLogger
将异常打印到控制台:

2016-01-20 16:39:36.8466 Error Error when writing to database System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
现在,我知道我可以设置
InternalLogger.LogFile()
,但我必须在Azure Web应用程序上托管的Web服务中使用它,并且在我尝试时它不会创建任何文件(我必须使用相对路径,它在控制台应用程序中本地工作,但在部署到Azure后不会)


使用异步NLog logger时如何处理连接异常?

您可以尝试:带有文件和DB目标的回退包装器

大概是这样的:


这也应该适用于
,但我建议先在不使用异步的情况下测试它

编辑: 众所周知,即使
throweexceptions=true
,也不会引发异常。我们正在努力,看


现在,如果您在登录文件时遇到问题,您可以选择内存或方法调用目标?

谢谢。您的解决方案可以在本地工作,也可以使用异步。非常有用,但不幸的是,它在部署到Azure后不起作用:(.我猜应用程序权限一定有问题,我必须深入研究。最终我更喜欢抛出异常,因为我可以通知客户出了问题。但是,如果我只在Azure上创建该文件,您提出的解决方案就足够了。
2016-01-20 16:39:36.8466 Error Error when writing to database System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)