0% found this document useful (0 votes)
12 views

Logging & Error Handling

Logging & Error handling

Uploaded by

Shahid Rockzz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Logging & Error Handling

Logging & Error handling

Uploaded by

Shahid Rockzz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Logging with nlog

-> Install the Nugget package


-> Create a config file with nlog.config file
-> mentioned target name and level of logging in rules
-> Enable copy to bin folder
-> Add nlog extenstion method as logging providers to the project

LogLevel based on severity:


- Trace = 0 //Capture all the logs from trace to Critical
- Debug = 1
- Information = 2
- Warning = 3
- Error = 4
- Critical =5
- None = 6

- logger.LogTrace() //To log trace message


- logger.LogDebug() //To log Debug message
- logger.LogInformation() //To log Information message
- logger.LogWarning() //To log Warning message
- logger.LogError() //To log Error message
- logger.LogCritical() //To log Critical message

Exception & its methods in MVC

- If exception is handled in your application then it must be configured inside the


Application_Error(Object sender, EventArgs e) method
- All controller level - protected override void OnException(ExceptionContext filterContext)
- We can also create our own Exception Handler by inheriting from HandleErrorAttribute as in
the following:
Eg:
[myExceptionhandler]
controller name{ //some code; }

public class MyExceptionHandler : HandleErrorAttribute


{
public override void OnException(ExceptionContext filterContext)
{

Exception below properties & methods:


- Source
- HelpLink
- StackTrace // Actual Error
- TargetSite
- InnerException // Origin Exception
- Message
- GetBaseException()

-> Use below method to log the error


- Server.GetLastError();
- log4net.LogManager.GetLogger( System.Reflection.MethodBase.
GetCurrentMethod().DeclaringType)
- log.Error(exception.Message, exception) //To log the error
-> use System.IO.StreamWriter class to write Log on text file.

Types of Errors:
- Debug
- Info
- Error
- Fatal

Method to log the error to Event Viewer:


private static void LogToEventViewer(string log)
{
if (EventLog.SourceExists("PragimTech.com"))
{
// Create an instance of the eventlog
EventLog eventLog = new EventLog("PragimTech");
// set the source for the eventlog
eventLog.Source = "PragimTech.com";
// Write the exception details to the event log as an error
eventLog.WriteEntry(log, EventLogEntryType.Error);
}
}

Error handling in ASP.Net Core

-> 404 error example in ASP.NET Core:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)


{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseStaticFiles();

app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}

-> With the following line in place, if there is a 404 error, the user is redirected to /Error/404. The
placeholder {0}, in "/Error/{0}" will automatically receive the http status code.
Eg:
app.UseStatusCodePagesWithRedirects("/Error/{0}");

You might also like