如何编写此SQL Server错误日志

时间:2016-12-08 07:17:34

标签: c# asp.net .net sql-server service

我花了几十个小时试图弄清楚为什么我无法通过我正在编写的Windows服务连接到我的数据库并且正在2016-12-07 23:00:39.98 Logon Login succeeded for user 'thelion'. Connection made using SQL Server authentication. [CLIENT: <local machine>] 2016-12-07 23:00:39.99 Logon Error: 18456, Severity: 14, State: 38. 2016-12-07 23:00:39.99 Logon Login failed for user 'thelion'. Reason: Failed to open the explicitly specified database 'LrcPageTask'. [CLIENT: <local machine>] 2016-12-07 23:04:10.72 Logon Login succeeded for user 'thelion'. Connection made using SQL Server authentication. [CLIENT: <local machine>] 2016-12-07 23:04:10.73 Logon Error: 18456, Severity: 14, State: 38. 2016-12-07 23:04:10.73 Logon Login failed for user 'thelion'. Reason: Failed to open the explicitly specified database 'LrcPageTask'. [CLIENT: <local machine>] 2016-12-07 23:04:20.75 Logon Login succeeded for user 'thelion'. Connection made using SQL Server authentication. [CLIENT: <local machine>] 2016-12-07 23:04:20.75 Logon Error: 18456, Severity: 14, State: 38. 2016-12-07 23:04:20.75 Logon Login failed for user 'thelion'. Reason: Failed to open the explicitly specified database 'LrcPageTask'. [CLIENT: <local machine>] 下运行。错误的快照是

OnStart

调用我的 protected override void OnStart(string[] args) { EventLog.WriteEntry(ServiceName + " started"); //PageLoadTimer.Enabled = true; try { repo.Add(new PageDocument() { Html = "<p>Test</p>", PageType = 0, DateAdded = DateTime.Now, DateProcessed = null }); } catch(Exception e) { EventLog.WriteEntry(string.Format("Problem added a page document: {0}", e.Message)); } } 方法

Add

调用public class PageDocumentRepository : PageCRUD { public void Add(PageDocument pd) { using(var context = GetPageTaskContext()) { context.PageDocuments.Add(pd); context.SaveChanges(); } } } 方法

GetPageTaskContext

public abstract class PageCRUD { public LrcPageTaskEntities GetPageTaskContext() { return new LrcPageTaskEntities(); } } 就像

LrcPageTaskEntities

public partial class LrcPageTaskEntities : DbContext { public LrcPageTaskEntities() : base("name=LrcPageTaskEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<PageDocument> PageDocuments { get; set; } } 类似

  <connectionStrings>
    <!--<add name="LrcPageTaskEntities" connectionString="metadata=res://*/Models.LrcPageTask.csdl|res://*/Models.LrcPageTask.ssdl|res://*/Models.LrcPageTask.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DESKTOP-300NQR3\SQLEXPRESS;initial catalog=LrcPageTask;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />-->
    <add name="LrcPageTaskEntities" connectionString="metadata=res://*/Models.LrcPageTask.csdl|res://*/Models.LrcPageTask.ssdl|res://*/Models.LrcPageTask.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DESKTOP-300NQR3\SQLEXPRESS;initial catalog=LrcPageTask;integrated security=false;user id=thelion;password=trump4america;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

我的连接字符串就像

LocalSystem

我不知道这是否相关,但我正在使用Visual Studio 2013 Express for Web和SQL Server Express 2016.

解决这个谜团的任何帮助?

编辑:对于一些额外的可见性,这是​​我正在尝试使用的登录名:

enter image description here

如果我尝试使用Intregated Security=TrueUser Id下登录并且未指定Password / 2016-12-07 23:17:27.95 Logon Login succeeded for user 'NT SERVICE\SQLTELEMETRY$SQLEXPRESS'. Connection made using Windows authentication. [CLIENT: <local machine>] 2016-12-07 23:20:37.15 Logon Login succeeded for user 'WORKGROUP\DESKTOP-300NQR3$'. Connection made using Windows authentication. [CLIENT: <local machine>] 2016-12-07 23:20:37.16 Logon Error: 18456, Severity: 14, State: 38. 2016-12-07 23:20:37.16 Logon Login failed for user 'WORKGROUP\DESKTOP-300NQR3$'. Reason: Failed to open the explicitly specified database 'LrcPageTask'. [CLIENT: <local machine>] 2016-12-07 23:20:47.20 Logon Login succeeded for user 'WORKGROUP\DESKTOP-300NQR3$'. Connection made using Windows authentication. [CLIENT: <local machine>] 2016-12-07 23:20:47.20 Logon Error: 18456, Severity: 14, State: 38. 2016-12-07 23:20:47.20 Logon Login failed for user 'WORKGROUP\DESKTOP-300NQR3$'. Reason: Failed to open the explicitly specified database 'LrcPageTask'. [CLIENT: <local machine>] ,则无效(因此我注释掉了连接字符串的原因) )。如果我尝试这样做,我会收到一系列登录错误

while(1)

2 个答案:

答案 0 :(得分:0)

州38意味着'Login valid but database unavailable (or login not permissioned)'

如果您想在错误日志中阅读有关每个州的更多信息,请通过 Aaron Bertrand 阅读此博客Troubleshooting Error 18456

确保数据库存在且login具有对数据库具有适当权限的用户。

答案 1 :(得分:0)

以下问题排查步骤取自:Troubleshoot Connectivity/Login failures (18456 State x) with SQL Server

链接中与您相关的几个故障排除步骤:

1.Default Trace:
您可以使用以下查询来查询默认跟踪

DECLARE @trcpath nvarchar(256)

SELECT  @trcpath=
CAST(value as nvarchar(256)) 
FROM  fn_trace_getinfo(default)  WHERE  property = 2

---Change “default” to 1 if you want to read
 information only from current trace file
  SELECT  * FROM fn_trace_gettable (@trcpath,default )
 WHERE  EventClass= 20
 ORDER BY starttime DESC 

输出将是下面的东西..

  

用户'del'登录失败。原因:无法打开登录属性中指定的数据库。 [客户:]   用户'sa'登录失败。原因:密码与提供的登录名不匹配。 [客户:]

2.Ring Buffer和Extended events:
您还可以使用ringbuffer DMV和Err.exe进行故障排除并获取失败原因..

在DMV下运行,会给你一些错误代码..

SELECT dateadd (ms, (a.[Record Time] - sys.ms_ticks), GETDATE()) as [Notification_Time], a.* FROM 
(SELECT 
x.value('(//Record/@id)[1]', 'bigint') AS [Record_ID], 
x.value('(//Record/Error/ErrorCode)[1]', 'varchar(30)') AS [ErrorCode], 
x.value('(//Record/Error/APIName)[1]', 'varchar(255)') AS [APIName], 
x.value('(//Record/Error/CallingAPIName)[1]', 'varchar(255)') AS [CallingAPIName], 
x.value('(//Record/Error/SPID)[1]', 'int') AS [SPID], 
x.value('(//Record/@time)[1]', 'bigint') AS [Record Time] 
FROM (SELECT CAST (record as xml) FROM sys.dm_os_ring_buffers 
WHERE ring_buffer_type = 'RING_BUFFER_SECURITY_ERROR') AS R(x)) a 
CROSS JOIN sys.dm_os_sys_info sys 
ORDER BY a.[Record_ID] DESC

如果它给出了例如 0x8C2 ,则可以使用Err.exe来获取如下所示的错误消息。

Err.exe 0x8C2

给出

# for hex 0x8c2 / decimal 2242 :
  NERR_PasswordExpired                                          lmerr.h

然后您可以在命令提示符中使用NETHELP MSG并获取更多详细信息,例如..

NET HELPMSG 2242

给出

  

此用户的密码已过期

     

说明

     

您的密码已过期。在更改密码之前,您将无法执行任何网络任务。

     

ACTION

     

要更改密码,请按Ctrl + Alt + Del,然后选择更改密码。

<强>参考文献:
https://blogs.msdn.microsoft.com/sqlsakthi/2011/02/06/troubleshoot-connectivitylogin-failures-18456-state-x-with-sql-server/