//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Configuration;
using System.Diagnostics;
namespace Trade.Utility.Loggers
{
public class EventViewerLogger : ILogger
{
private string logSource;
public EventViewerLogger()
{
logSource = ConfigurationManager.AppSettings.Get("EVENT_LOG");
}
#region ILogger Members
public void WriteDebugMessage(string message)
{
LogMessage(message, EventLogEntryType.Information, logSource);
}
public void WriteErrorMessage(string message)
{
LogMessage(message, EventLogEntryType.Error, logSource);
}
public void WriteException(Exception ex)
{
LogMessage(ex.ToString(), EventLogEntryType.Error, logSource);
}
#endregion
/// Writes to event log.
/// String with message to display/log.
/// Event Log entry type code
/// The event log source name
private void LogMessage(string message, EventLogEntryType messageType, string eventLog)
{
try
{
EventLog EventLog1 = new EventLog();
EventLog1.Source = eventLog;
EventLog1.WriteEntry(message, messageType);
}
catch (Exception e)
{
//Choice here: sometimes Web apps will throw a security exception
//writing to the event log if the source is not setup properly and the account 'Network Service'
//given "Full Control" to the event log to be able to write entries. Really, this exception should
//be thrown so user knows they are not getting event log info; however, to avoid frustration
//for those that get it, I am simply trapping here. Otherwise, you may wish to uncomment this line:
//
// throw new Exception("An exception occurred attempting to write to the Windows Application Event Log. Please make sure the 'Network Service' account (the account ASP.NET runs under) has write access to the Application Event Log. You will use RegEdit to add permissions, refer to the StockTrader Readme.html for precise instructions.\nThe specific exception writing to the " + EVENT_LOG + " Application Log event source is: " + e.ToString());
//
//So if you catch an exception here, check e to see if a security exception. If so, likely need to add Network
//Service to EventLog permissions via regedit, current control set, services, Event Log (select), then
//from main menu choose Permissions to add Network Service with Full Control (so ASP.NET/IIS can create/and write to).
string throwaway = e.Message;
}
}
}
}