Davor Sagner
Davor Sagner

Reputation: 25

Write new entry into SQL Server log file via asp.net c#

I have been at this for a few stressful hours and have given up on trying to find a question answered or problem solved for this particular approach I had in mind.

Thing is, I have been given a quite simple task to complete for a job application, but ran into a wall just at the end.

I am to develop an ASP.NET c# web application that shows a GridView with all entries in a SQL Server database table called Tasks which looks like this:

ID int IDENTITY(1,1) PRIMARY KEY, 
Title nvarchar(50) not null, 
DateOpened datetime not null, 
Status bit not null DEFAULT (0) --> opened/closed, 
DateClosed datetime null, 
Description nvarchar(500) null 

I will add the sql needed to understand better it's just I don't have it right now. Also the code you might think is most helpful to avoid a mile long question with lots of copy-pasted code. I didn't wait for it to become available just because I think the code itself would help little to nothing.

I am looking for a way to write an entry to the SQL Server Log File after a user attempts to insert a new Task with the same name AND status as an already existing one.

Basically, if there is an entry in the table like this:

Title 1, opened

and there is another one named Title 1 with status opened being insert into Tasks, the user should be alerted of the attempt, and it should be logged into SQL Server Log File.

I've never done such a thing, so I assume it should be somehow done from the Default.aspx.cs file inside the ValidateNewTask(Task t) method, where I run a foreach loop against all fetched entries, and if a "hit" appears, redirect the user to an error page or maybe keep it all on the same page with an error label since it's not that severe (like a RequiredFieldValidator would do), and THEN access the log to write the new entry of an error occurred.

Am I missing something that's right in front of me, or is this just not ever done (like this/ or at all)?

Upvotes: 0

Views: 1203

Answers (1)

Rhys Jones
Rhys Jones

Reputation: 5508

You can use RAISERROR ... WITH LOG to raise an error (the user will receive a SqlException) and write it to the SQL Server log file but this is not something I would recommend. The SQL Server log file is for SQL Server, if your application needs a log file I would create something separate and independent. Note the comments in the MSDN documentation about being SA or having ALTER TRACE to use WITH LOG.

Upvotes: 1

Related Questions