locked
Log events to Event Viewer

    Question

  • Simple question, I want to log some events to the Event Viewer. I've seen there's an example app for doing that but just for C#.

    Is there a way of logging to the event viewer with javascript? I'm using WinJS.log wich works well while debugging but I need something to tell the user in case there's an error.

    Thanks


    http://bit.ly/sebagomez

    Thursday, January 17, 2013 4:09 PM

Answers

  • Hi sebastian and got.dibbs,

    For logging into windows event log, there is no directly support in WinJS javascript API (Windows Runtime component). And I've also considered creating a .NET windows runtime component which wraps the call to System.Diagnostics.Eventlog class. However, it seems the .NET Framework for Windows Store apps hasn't included the EventLog class, therefore this approach also not work.

    And in the end, I found that there is raw windows API (for writing into event log) supported in Windows Store app. Therefore, we can consider creating C++ Windows runtime component which wraps the call to the event log related windows API functions. The following thread also mentioned this:

    #System.Diagnostics.EventLog replacement?
    http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/73c7fb3a-2183-4d60-b2b1-ff4aa9286700


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 31, 2013 7:09 AM
    Moderator

All replies

  • Hi Sebastian,

    I would like to suggest you display the error message(or other type info) in a specific div(named statusMessage in the following example)

    the javascript looks like:

    function ClickEventHandler(eventInfo) {      
            WinJS.log && WinJS.log("error message", "sample", "status");
        }
    
        var lastError = "";
        var lastStatus = "";
        WinJS.log = function (message, tag, type) {
            var isError = (type === "error");
            var isStatus = (type === "status");
    
            if (isError || isStatus) {
                var statusDiv = /* @type(HTMLElement) */ document.getElementById("statusMessage");
                if (statusDiv) {
                    statusDiv.innerText = message;
                    if (isError) {
                        lastError = message;
                        statusDiv.style.color = "blue";
                    } else if (isStatus) {
                        lastStatus = message;
                        statusDiv.style.color = "green";
                    }
                }
            }
        };
    please remember to register the event handler ClickEventHandler for your button when app launches.

    Hope this helps, thanks.


    Yanping Wang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, January 18, 2013 3:24 AM
    Moderator
  • I do have that... but I'd like a way to send info to the Event Viewer.

    Is that not suggested for Windows Store apps?


    http://bit.ly/sebagomez

    Friday, January 18, 2013 1:04 PM
  • I have the exact same question. Anyone have a thought on how to do this?
    Saturday, January 19, 2013 6:11 PM
  • Hi,

    You can write a custome function to store the error log. And with javascript, there are many ways. For details, please refer to my blog: http://blogs.msdn.com/b/win8devsupport/archive/2012/12/19/using-html5-javascript-in-windows-store-apps-data-access-and-storage-mechanism-i.aspx .


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 24, 2013 10:55 AM
  • Roy, so what you're saying is that there isn't any way to log to the event viewer?

    We've tried logging to a local store, and logging "to the DOM" like in Eight.js but that isn't as friendly nor as normalized of an experience as it would be to log to the event viewer.


    Thursday, January 24, 2013 1:50 PM
  • Hi sebastian and got.dibbs,

    For logging into windows event log, there is no directly support in WinJS javascript API (Windows Runtime component). And I've also considered creating a .NET windows runtime component which wraps the call to System.Diagnostics.Eventlog class. However, it seems the .NET Framework for Windows Store apps hasn't included the EventLog class, therefore this approach also not work.

    And in the end, I found that there is raw windows API (for writing into event log) supported in Windows Store app. Therefore, we can consider creating C++ Windows runtime component which wraps the call to the event log related windows API functions. The following thread also mentioned this:

    #System.Diagnostics.EventLog replacement?
    http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/73c7fb3a-2183-4d60-b2b1-ff4aa9286700


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 31, 2013 7:09 AM
    Moderator
  • There is currently no way. However we are going to write a log4javascript Appender for WinRT event log. Stay tuned.
    Tuesday, February 5, 2013 9:28 AM
  • This will write to the event viewer.

    WinJS.Utilities.startLog({ type: 'error', tags: 'myTag' })
    
    WinJS.log && WinJS.log('there was an error', 'myTag', 'error');
    

    A tricky part is finding the entry in the event viewer.  On my Windows 8.1 tablet, it is at:

    Application And Services\Microsoft\Windows\AppHost\Admin

    HTH


    Phil

    • Proposed as answer by Shmuel Englard Sunday, April 13, 2014 10:16 PM
    Thursday, February 27, 2014 6:49 PM