Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
Azure Diagnostic: How to write custom messages to event log?

Answered Azure Diagnostic: How to write custom messages to event log?

  • Thursday, September 20, 2012 1:28 AM
     
      Has Code

    I've enabled diagnostics (per my answer here). Now when I try to add a custom message to the log by:

    in Global.asax (after diagnostic init obviously!)

    EventLog.WriteEntry("Demo Webrole", "This is a message from global.asax", EventLogEntryType.Information);

    I get a security exception as

    Security Exception

    Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.           

    Exception Details: System.Security.SecurityException: The source was not found, but some or all event logs could not be searched.  To create the source, you need permission to read all event logs to make sure that the new source name is unique.  Inaccessible logs: Security.

    So now it seems the default Windows Azure configuration won't allow log creation and I don't want to fight the platform by hacking priviledges etc (.NET trust level is "Full" by the way).

    Our requirement is simple: We need a simple cloud aware diagnostic infrastructure so we can focus on the business logic itself.

    So my question is: What is the simplest, straightforward way to get log messages out from the application into some persistant place? Code or links to working tutorials would be great!



    • Edited by Sid.S Thursday, September 20, 2012 2:38 AM
    •  

All Replies

  • Thursday, September 20, 2012 7:13 AM
     
     Proposed

    Hi,

    The easiest way to work with logging is use Ent lib 5.0/6.0. You should be good. We have used the same in our all our applications.

    Configure you diagnostics log transfer interval for each source you are logging.

    for more info on using ent lib see this link, blog

    With this you worry on about your application logic and logging will be taken care by application blocks.

    Hope it helps.



    Please mark the replies as Answered if they help and Vote if you found them helpful.


  • Thursday, September 20, 2012 9:07 AM
     
     Answered

    Hi,

    faced this situation when the application tried to create a new Event Source for my Event Log messages. For doing this, the application needs admin priveleges. To work around this issue, we created a startup task which helped us create the Event Source. That way, when the actual logging happened, the said Event Source was already created and ready for use. This is what our startp task contained:

    EventCreate /L Application /T Information /ID 100 /SO "MyEventSourceName" /D "Created my custom source for logging my events" >> "%TEMP%\MyLog.txt" 2>&1

    You can read step-by-step guide on this at http://coding.infoconex.com/post/2012/02/09/Creating-custom-EventLog-source-in-windows-azure.aspx

    Also some recommendations on best practices for startup tasks can be found at http://blog.smarx.com/posts/windows-azure-startup-tasks-tips-tricks-and-gotchas

    HTH

    • Proposed As Answer by Pooja Singh Thursday, September 20, 2012 9:07 AM
    • Marked As Answer by Sid.S Thursday, September 20, 2012 7:31 PM
    •  
  • Thursday, September 20, 2012 7:38 PM
     
     

    Thanks Pooja, I've marked it as an answer although we didn't test it.

    We ended up going the route of writing custom logs directly to a file as we were doing before. The only difference is that this time we're writing to the file inside an Azure "Local Resource" and we're using WAD to move the Local Resource folder into an Azure Blob. I might look into writing straight to Azure tables since our log format is essentially a table format but only if it doesn't take more than 20 minutes or so. And since our logging and tracing is managed centrally (in code) it should be easy to swap it with the official method when the WAD tools and infrastructure mature (and there is solid documentation on how to consume it).


    • Edited by Sid.S Thursday, September 20, 2012 7:39 PM
    •