我一直在嘗試使用NLog獲取ASP.NET Core站點。它工作正常,直到我嘗試寫入SQL數據庫。我試過本地數據庫和Azure數據庫 - 所有這些都有相同的問題。我甚至將nlog表添加到一個已知的數據庫中,一個網站已經連接到(使用EF)。寫入數據庫時NLog,ASP.NET核心和SQL - 服務器未找到或無法訪問
錯誤錯誤:
我使用的NuGet包:NLog.Web.AspNetCore
不管是什麼,我得到以下。異常: System.Data.SqlClient.SqlException(0x80131904):與SQL Server建立連接時出現與網絡有關的或 實例特定的錯誤。服務器未找到或無法訪問。驗證 實例名稱是否正確,並且SQL Server配置爲允許遠程連接的 。
這裏是我如何配置的東西(我試圖改變這些語句的順序):
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddNLog();
app.AddNLogWeb();
env.ConfigureNLog("nlog.config");
LogManager.Configuration.Variables["connectionString"] = Configuration.GetConnectionString("DefaultConnection");
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
//etc...
這裏是我的n日誌配置:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="c:\temp\internal-nlog.txt">
<!-- define various log targets -->
<targets>
<target name="db"
xsi:type="Database"
commandType="StoredProcedure"
commandText="[dbo].[NLog_AddEntry_p]">
<parameter name="@machineName" layout="${machinename}" />
<parameter name="@siteName" layout="${iis-site-name}" />
<parameter name="@logged" layout="${date}" />
<parameter name="@level" layout="${level}" />
<parameter name="@username" layout="${aspnet-user-identity}" />
<parameter name="@message" layout="${message}" />
<parameter name="@logger" layout="${logger}" />
<parameter name="@properties" layout="${all-event-properties:separator=|}" />
<parameter name="@serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
<parameter name="@port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
<parameter name="@url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
<parameter name="@https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />
<parameter name="@serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
<parameter name="@remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
<parameter name="@callSite" layout="${callsite}" />
<parameter name="@exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="db" />
</rules>
</nlog>
末,這裏是我的appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\projectsV13;Database=XXXX;Trusted_Connection=True;MultipleActiveResultSets=true",
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
這是正確的@Julian。我曾經讀過在目標中使用連接字符串的地方。所以相反,我使用這個:LogManager.Configuration.Variables [「connectionString」] = Configuration.GetConnectionString(「DefaultConnection」);我將再次嘗試使用nlog config中目標節點中的數據。 – Schwammy
你還需要'LogManager.Configuration.Variables [「connectionString」] = Configuration.GetConnectionString(「DefaultConnection」);' - 你需要(config和C#) – Julian
Both?好吧,我會嘗試!我在測試後會在這裏更新。 – Schwammy