[ComImportAttribute()][GuidAttribute("00230206-B1BA-11CE-ABC6-F5B2E79D9E3F")][InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType)][TypeLibTypeAttribute(1)]public interface ILtmsLogHandler
Once the application has defined its own log handler it can pass a pointer to its ILtmsLogHandler interface using the ILogHandler property.
The server will call the application, and log events through the assigned log handler interface.
using Leadtools;using Leadtools.MediaStreaming;enum W3CLOG{date = 0x00000001,time = 0x00000002,c_ip = 0x00000004,cs_username = 0x00000008,s_sitename = 0x00000010,s_computername = 0x00000020,s_ip = 0x00000040,s_port = 0x00000080,cs_method = 0x00000100,cs_uri_stem = 0x00000200,cs_uri_query = 0x00000400,sc_status = 0x00000800,sc_win32_status = 0x00001000,sc_bytes = 0x00002000,cs_bytes = 0x00004000,time_taken = 0x00008000,cs_version = 0x00010000,cs_host = 0x00020000,cs_user_agent = 0x00040000,cs_cookie = 0x00080000,cs_referer = 0x00100000,sc_substatus = 0x00200000,};public class CW3CLogHandler : ILtmsLogHandler{bool m_enable;int m_fields;bool m_localtime;DateTime m_dtfile;string m_software;string m_folder;string m_prefix;StreamWriter m_sw;public CW3CLogHandler(string prefix, string software, bool enable, string folder, int fields, bool localtime){m_prefix = prefix;m_software = software;m_enable = enable;m_folder = folder;m_fields = fields;m_localtime = localtime;}~CW3CLogHandler(){}void WriteString(string str){m_sw.Write(str);}DateTime GetTime(double dTime){DateTime dt = DateTime.FromOADate(dTime);if (m_localtime)return dt.ToLocalTime();return dt;}string GetLogPath(double timestamp){DateTime dt = GetTime(timestamp);string strFullPath;try{PathResolver resolver = new PathResolver();strFullPath = resolver.Resolve(m_folder);}catch{return "";}strFullPath += "\\";strFullPath += m_prefix;if (!m_localtime){strFullPath += dt.ToString("y-MM-dd-HH-mm-ss").Replace("-", "") + "UTC.log";}else{strFullPath += dt.ToString("y-MM-dd-HH-mm-ss").Replace("-", "") + ".log";}return strFullPath;}public void LoadConfig(Server server){bool enable = m_enable;bool localtime = m_localtime;string folder = m_folder;int fields = m_fields;bool dirty = false;ApplicationProperties AppProps = server.GetApplicationProperties();try{enable = AppProps.GetBoolean("LogEnable");}catch{AppProps.AddBoolean("LogEnable", enable);dirty = true;}try{localtime = AppProps.GetBoolean("LogLocalTime");}catch{AppProps.AddBoolean("LogLocalTime", localtime);dirty = true;}try{folder = AppProps.GetString("LogFolder");}catch{AppProps.AddString("LogFolder", folder);dirty = true;}try{fields = AppProps.GetInteger("LogFields");}catch{AppProps.AddInteger("LogFields", fields);dirty = true;}if (dirty)server.SetApplicationProperties(AppProps);// if anything has changed then we need to close the current fileif (!enable && (string.Compare(m_folder, folder, true) != 0) || fields != m_fields || m_localtime != localtime){if (m_sw != null)m_sw.Close();}m_folder = folder;m_localtime = localtime;m_enable = enable;m_fields = fields;}bool CreateLog(double timestamp){if (m_sw != null)m_sw.Close();if (!m_enable)return false;try{m_sw = File.CreateText(GetLogPath(timestamp));}catch{return false;}m_dtfile = GetTime(timestamp);string s;s = "#Software: " + m_software + "\r\n";WriteString(s);WriteString("#Version: 1.0\r\n");DateTime dt = GetTime(timestamp);s = "#Date: " + dt.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";WriteString(s);s = "#Fields:";if ((m_fields & (int)W3CLOG.date) == (int)W3CLOG.date){s += " date";}if ((m_fields & (int)W3CLOG.time) == (int)W3CLOG.time){s += " time";}if ((m_fields & (int)W3CLOG.s_sitename) == (int)W3CLOG.s_sitename){s += " s-sitename";}if ((m_fields & (int)W3CLOG.s_computername) == (int)W3CLOG.s_computername){s += " s-computername";}if ((m_fields & (int)W3CLOG.s_ip) == (int)W3CLOG.s_ip){s += " s-ip";}if ((m_fields & (int)W3CLOG.cs_method) == (int)W3CLOG.cs_method){s += " cs-method";}if ((m_fields & (int)W3CLOG.cs_uri_stem) == (int)W3CLOG.cs_uri_stem){s += " cs-uri-stem";}if ((m_fields & (int)W3CLOG.cs_uri_query) == (int)W3CLOG.cs_uri_query){s += " cs-uri-query";}if ((m_fields & (int)W3CLOG.s_port) == (int)W3CLOG.s_port){s += " s-port";}if ((m_fields & (int)W3CLOG.cs_username) == (int)W3CLOG.cs_username){s += " cs-username";}if ((m_fields & (int)W3CLOG.c_ip) == (int)W3CLOG.c_ip){s += " c-ip";}if ((m_fields & (int)W3CLOG.cs_version) == (int)W3CLOG.cs_version){s += " cs-version";}if ((m_fields & (int)W3CLOG.cs_user_agent) == (int)W3CLOG.cs_user_agent){s += " cs(User-Agent)";}if ((m_fields & (int)W3CLOG.cs_cookie) == (int)W3CLOG.cs_cookie){s += " cs(Cookie)";}if ((m_fields & (int)W3CLOG.cs_referer) == (int)W3CLOG.cs_referer){s += " cs(Referer)";}if ((m_fields & (int)W3CLOG.cs_host) == (int)W3CLOG.cs_host){s += " cs-host";}if ((m_fields & (int)W3CLOG.sc_status) == (int)W3CLOG.sc_status){s += " sc-status";}if ((m_fields & (int)W3CLOG.sc_substatus) == (int)W3CLOG.sc_substatus){s += " sc-substatus";}if ((m_fields & (int)W3CLOG.sc_win32_status) == (int)W3CLOG.sc_win32_status){s += " sc-win32-status";}if ((m_fields & (int)W3CLOG.sc_bytes) == (int)W3CLOG.sc_bytes){s += " sc-bytes";}if ((m_fields & (int)W3CLOG.cs_bytes) == (int)W3CLOG.cs_bytes){s += " cs-bytes";}if ((m_fields & (int)W3CLOG.time_taken) == (int)W3CLOG.time_taken){s += " time-taken";}s += "\r\n";WriteString(s);m_sw.Flush();return true;}string escape(string s){string t = "";foreach (char c in s){if (!char.IsControl(c) && !char.IsWhiteSpace(c))t += c;elset += "+";}return t;}public void InitializeLog(double timestamp){CreateLog(timestamp);}public void TerminateLog(){if (m_sw != null)m_sw.Close();}public void LogInformation(ILtmsLogInformation info){string s = "";if (!m_enable)return;// check if we need to roll to a new file{DateTime dt = GetTime(info.TimeStamp);if (m_sw == null || dt.Day != m_dtfile.Day || dt.Month != m_dtfile.Month || dt.Year != m_dtfile.Year){if (!CreateLog(info.TimeStamp))return;}}if ((m_fields & (int)W3CLOG.date) == (int)W3CLOG.date){DateTime dt = GetTime(info.TimeStamp);if (s.Length > 0)s += " ";s += dt.ToString("yyyy-MM-dd");}if ((m_fields & (int)W3CLOG.time) == (int)W3CLOG.time){DateTime dt = GetTime(info.TimeStamp);if (s.Length > 0)s += " ";s += dt.ToString("HH:mm:ss");}if ((m_fields & (int)W3CLOG.s_sitename) == (int)W3CLOG.s_sitename){string str = info.SiteName;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.s_computername) == (int)W3CLOG.s_computername){string str = info.ComputerName;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.s_ip) == (int)W3CLOG.s_ip){string str = info.ServerIP;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.cs_method) == (int)W3CLOG.cs_method){string str = info.Method;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.cs_uri_stem) == (int)W3CLOG.cs_uri_stem){string str = info.URIStem;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.cs_uri_query) == (int)W3CLOG.cs_uri_query){string str = info.URIQuery;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.s_port) == (int)W3CLOG.s_port){int n = info.ServerPort;if (s.Length > 0)s += " ";s += n.ToString();}if ((m_fields & (int)W3CLOG.cs_username) == (int)W3CLOG.cs_username){string str = info.UserName;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.c_ip) == (int)W3CLOG.c_ip){string str = info.ClientIP;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.cs_version) == (int)W3CLOG.cs_version){string str = info.ProtocolVersion;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.cs_user_agent) == (int)W3CLOG.cs_user_agent){string str = info.UserAgent;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.cs_cookie) == (int)W3CLOG.cs_cookie){string str = info.Cookie;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.cs_referer) == (int)W3CLOG.cs_referer){string str = info.Referrer;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.cs_host) == (int)W3CLOG.cs_host){string str = info.Host;if (s.Length > 0)s += " ";if (str.Length == 0)s += "-";elses += escape(str);}if ((m_fields & (int)W3CLOG.sc_status) == (int)W3CLOG.sc_status){int n = info.Status;if (s.Length > 0)s += " ";s += n.ToString();}if ((m_fields & (int)W3CLOG.sc_substatus) == (int)W3CLOG.sc_substatus){int n = info.ProtocolSubStatus;if (s.Length > 0)s += " ";s += n.ToString();}if ((m_fields & (int)W3CLOG.sc_win32_status) == (int)W3CLOG.sc_win32_status){int n = info.Win32Status;if (s.Length > 0)s += " ";s += n.ToString();}if ((m_fields & (int)W3CLOG.sc_bytes) == (int)W3CLOG.sc_bytes){int n = info.BytesSent;if (s.Length > 0)s += " ";if (n < 0)s += "-";elses += n.ToString();}if ((m_fields & (int)W3CLOG.cs_bytes) == (int)W3CLOG.cs_bytes){int n = info.BytesReceived;if (s.Length > 0)s += " ";if (n < 0)s += "-";elses += n.ToString();}if ((m_fields & (int)W3CLOG.time_taken) == (int)W3CLOG.time_taken){int n = info.TimeTaken;if (s.Length > 0)s += " ";if (n < 0)s += "-";elses += n.ToString();}s += "\r\n";WriteString(s);m_sw.Flush();}}public Server _server = null;public bool _result = false;public void W3CLogHandlerExample(){try{State enumState;CW3CLogHandler _loghandler;// create an instance of the server object_server = new Leadtools.MediaStreaming.Server();// create the log handler_loghandler = new CW3CLogHandler("ltmsServer_", "LEADTOOLS Media Streaming Server", true, "%ltmsLogFolder%", 0, false);// set the log handler_server.ILogHandler = _loghandler;{// for demonstration, compare the interface we just setILtmsLogHandler ihandler = null;ihandler = _server.ILogHandler;if (ihandler != (ILtmsLogHandler)_loghandler){_result = false;return;}}// Run with the server's default settings// or uncomment this section to load the config file located in the config subfolder under the executable folder//_server.ImportConfigFile("%ltmsConfigFolder%\\LeadtoolsMediaStreamingServer.xml");// start the server_server.Start();// confirm the running state for demonstration purposesenumState = _server.State;if (enumState == State.Started){// display a message that the server is running and wait for OKMessageBox.Show("The server has started. Press OK to stop.", "LEADTOOLS Media Streaming Examples", MessageBoxButtons.OK, MessageBoxIcon.Information);}// stop the server_server.Stop();// remove the log handler_server.ILogHandler = null;_result = true;}catch (Exception){_result = false;}}