14

For debugging purposes, how can I print to the event log/viewer in DotNetNuke, using VB.NET or C#?

Since_2008
  • 2,331
  • 8
  • 38
  • 68

4 Answers4

24

From http://www.ventrian.com/Resources/Articles/tabid/213/articleType/ArticleView/articleId/330/Logging-to-the-EventLog.aspx (just the relevant part of the article):

Using the event log in code is quite simple, the code is as follows:-

First, create an instance of the EventLogViewer...

Dim objEventLog As New DotNetNuke.Services.Log.EventLog.EventLogController

Next, log the event you wish to trap...

objEventLog.AddLog("Sample Message",
    "Something Interesting Happened!",
    PortalSettings,
    -1,
    DotNetNuke.Services.Log.EventLog.EventLogController.EventLogType.ADMIN_ALERT)

Now, when this code is run, the event log entry should appear in admin -> log viewer! You can customise these type of events, whether they be admin, host, item updated, etc.

bdukes
  • 152,002
  • 23
  • 148
  • 175
GalacticCowboy
  • 11,663
  • 2
  • 41
  • 66
8

Also, if you want to add more data to the log, you can use LogInfo class to add events to the log.

Dim eventLog As EventLogController
eventLog = New EventLogController()

Dim logInfo As DotNetNuke.Services.Log.EventLog.LogInfo
logInfo = New LogInfo()
logInfo.LogUserID = UserId
logInfo.LogPortalID = PortalSettings.PortalId
logInfo.LogTypeKey = EventLogController.EventLogType.ADMIN_ALERT.ToString()
logInfo.AddProperty("PropertyName1", propertyValue1)
logInfo.AddProperty("PropertyName2", propertyValue2)

eventLog.AddLog(logInfo)
skajfes
  • 8,125
  • 2
  • 24
  • 23
5

This is the C# version

using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Log.EventLog;

EventLogController eventLog = new EventLogController();
DotNetNuke.Services.Log.EventLog.LogInfo logInfo = new LogInfo();
logInfo.LogUserID = UserId;
logInfo.LogPortalID = PortalSettings.PortalId;
logInfo.LogTypeKey=EventLogController.EventLogType.ADMIN_ALERT.ToString();
logInfo.AddProperty("KeyWord=", txtSearch.Text.Trim());
logInfo.AddProperty("KeyWordLike=", myParams);
eventLog.AddLog(logInfo);
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Giridhar
  • 95
  • 2
  • 4
0
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Log.EventLog;
public static void DnnEventLogMsg(string msg)
{
    EventLogController eventLog = new EventLogController();
    DotNetNuke.Services.Log.EventLog.LogInfo logInfo = new LogInfo();
    PortalSettings ps = PortalController.Instance.GetCurrentPortalSettings();
    UserInfo userInfo = UserController.Instance.GetCurrentUserInfo();
    logInfo.LogUserID = userInfo.UserID;
    logInfo.LogPortalID = ps.PortalId;
    logInfo.LogTypeKey = EventLogController.EventLogType.ADMIN_ALERT.ToString();
    logInfo.AddProperty("Atena3 Message", msg);
    eventLog.AddLog(logInfo);
}
Tone Škoda
  • 1,463
  • 16
  • 20