0

I am re-writing some programs to be of better architecture. They are small, relatively simple multi-project solutions in VS 2017 consisting of a GUI in WinForms, a BLL class library, and a DAL class library - all in VB.NET.

everything is working well, I have few issues. I am using NLog for logging to a DB and it is working well. It is currently only logging on one layer, the GUI. I am throwing all exceptions to the top layer and catching them there, and logging them there.

I want to change how I use NLog and separate it from any of the GUI, BLL, and DAL layers - migrate it to a 4th layer - a utility class library layer.

This is what I have done:

Created a utility class library, with an interface and a public class. The public class inherits NLog Logger and impliments the interface:

Public Interface ILogger
Sub Info(message As String)
Sub Info(ex As Exception, message As String)

Sub Debug(message As String)
Sub Debug(ex As Exception, message As String)

Sub [Error](message As String)
Sub [Error](ex As Exception, message As String)
End Interface

Then, the class:

Imports NLog
Public Class MyLogger
Inherits Logger
Implements ILogger
Public Shared _log As Logger = LogManager.GetLogger("databaseLogger")
Public Sub Debug(message As String) Implements ILogger.Info
    _log.Debug(message)
End Sub
Public Sub Debug(ex As Exception, message As String) Implements ILogger.Info
    _log.Debug(ex, message)
End Sub

Public Sub Info(message As String) Implements ILogger.Debug
    _log.Info(message)
End Sub
Public Sub Info(ex As Exception, message As String) Implements ILogger.Debug
    _log.Info(ex, message)
End Sub

Public Sub [Error](message As String) Implements ILogger.Error
    _log.Error(message)
End Sub
Public Sub [Error](ex As Exception, message As String) Implements ILogger.Error
    _log.Error(ex, message)
End Sub
End Class

I then made a winform project in the same solution, referenced the utility class, and stuck this in the startup form:

Public Class Form1
Private _logger As LogTest2.ILogger

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    _logger.Debug("test")
End Sub
End Class

However, When I run it I am getting am error:

'Object reference not set to an instance of an object.'

I am obviously missing something - I've done quite a bit of searching around SO, and while there are many questions all showing C# examples, I see few VB.NET examples. Can someone help me out?

SkylaurRoe
  • 35
  • 1
  • 9
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jmcilhinney Nov 30 '18 at 01:52
  • I suspect that you're over-thinking this particular issue. The first two code snippets appear irrelevant to the issue and your problem is exactly like any other when this exception is thrown: you're trying to access a member of an object via a variable that you have never assigned an object to. – jmcilhinney Nov 30 '18 at 01:53
  • I've never used an interface before - for some reason I thought it behaved like a shared sub but using the class that I implemented it on. My whole objective is to get logging working on multiple layers while only using one instance of it, and de-coupling the logging from the code using the interface. It's because I don't understand interfaces that well yet, and I'm not even sure if I am taking the right approach. Am I anywhere near using the right approach? – SkylaurRoe Nov 30 '18 at 03:11
  • An interface is very much not that. Your talking about a module. A module is like a class (and in fact is one after compilation) where every member is `Shared`. An interface doesn't actually provide any functionality at all, but rather species a set of functionality that classes must implement to be considered to implement that interface. For instance, binding to a `DataGridView` requires an object that implements `IList` or `IListSource`. It does matter what the actual type is as long as it implements one of those interfaces. Internally, binding treates the object as the interface. – jmcilhinney Nov 30 '18 at 03:18

0 Answers0