How to use Logger inside a class?

How to use Logger inside a class?

j.pavlicek
Collaborator Collaborator
816 Views
3 Replies
Message 1 of 4

How to use Logger inside a class?

j.pavlicek
Collaborator
Collaborator

Hello,

Consider code:

Class ThisRule
Sub Main
    Dim x As new CustomClass
    x.Log("abc")
End Sub  
End Class

Class CustomClass
    Public Sub Log(byval Value as String)
        Logger.Debug(Value) 'Line 10
    End Sub
End Class

Throws error:

Error on Line 10 : Logger is not declared. It may be inaccessible due to its protection level (this is translation from non-english system)

 

How to use Logger inside a class. What is Logger member of? (I tried Inventor.Logger, iLogic.Logger without succes.)



Inventor 2022, Windows 10 Pro
Sorry for bad English.
0 Likes
Accepted solutions (3)
817 Views
3 Replies
Replies (3)
Message 2 of 4

JelteDeJong
Mentor
Mentor
Accepted solution

It looks like you lose the normal logger as soon as you add a custom class to your rule. But you can still access the LogControl object. This object you can pass to your custom class through the constructor. That would look something like this.

Public Class ThisRule 	
	Public Sub Main() 
		Dim logControl As LogControl =  iLogicVb.Automation.LogControl
		
		logControl.Log(LogLevel.Debug, "HelloWorld from ThisRule")
		
	    Dim x As New CustomClass(logControl)
	    x.Log("HelloWorld from CustomClass")

	End Sub	
End Class 

Class CustomClass
	Private _logControl As LogControl
	
	Public Sub New(logger As LogControl)		
		_logControl = logger		
	End Sub
	
    Public Sub Log(ByVal Value As String)
        _logControl.Log(LogLevel.Debug,Value) 
    End Sub
End Class

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 4

Michael.Navara
Advisor
Advisor
Accepted solution

I found similar solution. Logger is automaticaly generated property of ThisRule during compile time. You can use it in sub class, but you need to send reference to it.

I don't know if this is bug or feature, but when you use argument name "logger" the property Logger is not generated. See this example

 

 

Sub Main
	Logger.Debug("main")

	Dim oSubClass As New SubClass(Logger)
	oSubClass.TestLog("test")
End Sub

Class SubClass
	Private log As IRuleLogger

''   Uncomment this constructor raises a compile error at line 2
'	Public Sub New(logger As IRuleLogger, b As Boolean)
'		Me.log = logger
'	End Sub

	Public Sub New(log As IRuleLogger)
		Me.log = log
	End Sub

	Public Sub TestLog(msg As String)
		log.Info(msg)
	End Sub
End Class

 

Message 4 of 4

C_Haines_ENG
Collaborator
Collaborator
Accepted solution

I know its an old thread but I figured id add to it. 

 

Sub Main
    CustomClass.iLog = Logger
    Dim x As new CustomClass
    x.Log("abc")
End Sub  

Class CustomClass
    Public Shared iLog as iRuleLogger
    Public Sub Log(byval Value as String)
        iLog.Debug(Value) 'Line 10
    End Sub
End Class