Skip to main content
  1. Posts/

Day 43 - Happy New Year

OldDays drag-spin-exp SQL csharp

Retroactive Resolution for 2016: Take a solid week off at the beginning of the year before blogging again.

In truth, I’ve simply lost a bit of time to work and illness and Fallout 4.

Coding
#

Ok, now to make the server put its log in the database, so we can test it more easily. A table in the database:

DragonsSpine/SQL Scripts/Log.sql
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
CREATE TABLE [dbo].[Log](
  [id] [int] IDENTITY(1,1) NOT NULL,
  [logtime] [datetime] NOT NULL,
  [logtype] [nvarchar](50) NULL,
  [message] [nvarchar](max) NULL,
 CONSTRAINT [PK__Log__03A67F89] PRIMARY KEY NONCLUSTERED 
(
  [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [dbo].[Log] ADD  CONSTRAINT [DF_Log_LogTime]  DEFAULT (getdate()) FOR [logtime]
GO

and a stored procedure to add entries:

DragonsSpine/SQL Scripts/prApp_Log_Add.sql
18
19
20
21
22
23
24
25
26
27
28
CREATE PROCEDURE [dbo].[prApp_Log_Add] 
  -- Add the parameters for the stored procedure here
  @logtype nvarchar(50) = '', 
  @message nvarchar(MAX) = ''
AS

  -- Insert statements for procedure here
  INSERT Log(logtype, message)
  VALUES (@logtype, @message);
  
GO

and the code to call it:

DragonsSpine/DAL/DBLog.cs
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
internal static int addLogEntry(string message, string logType)
{
  try
  {
    SqlStoredProcedure sp = new SqlStoredProcedure("prApp_Log_Add", DataAccess.GetSQLConnection());
    sp.AddParameter("@logtype", SqlDbType.NVarChar, 50, ParameterDirection.Input, logType);
    sp.AddParameter("@message", SqlDbType.NVarChar, 250, ParameterDirection.Input, message);

    int rc = sp.ExecuteNonQuery();
    // Console.WriteLine("addLogEntry rows affected: " + rc + ".");

    return rc;
  }
  catch (Exception e)
  {
    Utils.LogException(e);
    Console.WriteLine("addLogEntry exception " + e.ToString() + ".");
    return -1;
  }
}

(Are we really creating a new connection for each log call? For the moment, yes.)

DragonsSpine/GameSystems/Utilities/Utils.cs
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
try
{
  int result = DAL.DBLog.addLogEntry(message, logType.ToString());
  if (result < 1)
    Console.WriteLine(DateTime.Now.ToString() + ": Utils.Log(" + message + ", " + logType.ToString() + ") database entry result is " + 
              result + ".");
}
catch (System.IO.IOException)
{
  Console.WriteLine(DateTime.Now.ToString() + ": Threw an IOException at Utils.Log(" + message + ", " + logType.ToString() + ").");
  Console.Write("> ");
}
catch (Exception)
{
  Console.WriteLine(DateTime.Now.ToString() + ": Threw an Exception at Utils.Log(" + message + ", " + logType.ToString() + ").");
  Console.Write("> ");
}

(Will I ever get an IOException here? Doubtful. Admitted cut-and-paste from other pieces of logging code.)

And presto:

Now this we can use.
Now this we can use.

So let’s add some other saved procedures for easy access:

DragonsSpine/SQL Scripts/prApp_Log_Get_MaxID.sql
16
17
18
19
20
21
22
23
24
CREATE PROC [dbo].[prApp_Log_Get_MaxID]

AS

SELECT TOP 1 *
  FROM Log
  ORDER BY id DESC

GO
DragonsSpine/SQL Scripts/prApp_Log_By_MinID.sql
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
CREATE PROC [dbo].[prApp_Log_By_MinID]
  @minId int

AS

SELECT  *
FROM    Log
WHERE id >= @minId
ORDER BY id

GO

That should make it easy to remember a point in time, run a test, and then grab everything that happened during it. Note my avoidance of using the timestamps, which can sometimes be a pain to deal with. (Should I have made the ID something larger than int? Are the logs that busy? Time will certainly tell.)

Fandom and Jamming and Learning and Stuff
#


More to come
More to come

Day 43 code - server