private void LogConnectorStats()
        {
            List<string> fields = new List<string>
                { "Msgs_Sent", "Msgs_Rcvd", "Bytes_Sent", "Bytes_Rcvd",
                  "Msgs_Sent_Per_Sec", "Msgs_Rcvd_Per_Sec", "Bytes_Sent_Per_Sec", "Bytes_Rcvd_Per_Sec",
                 "Queued_Msgs",
                 "UpdatedProperties_Sent", "UpdatedProperties_Rcvd",
                 "NewObject_Sent", "NewObject_Rcvd", "NewPresence_Sent", "NewPresence_Rcvd"
                };

            // Milliseconds since the last time we collected statistics
            int msSinceLast = Util.EnvironmentTickCountSubtract(lastStatTime);

            SortedDictionary<string, SortedDictionary<string, Stat>> DSGStats;
            if (StatsManager.TryGetStatsForCategory(DSGDetailCategory, out DSGStats))
            {
                foreach (string container in DSGStats.Keys)
                {
                    LogWriter connWriter = null;
                    SyncConnectorStat lastStat = null;
                    Dictionary<string, double> outputValues = new Dictionary<string, double>();

                    SortedDictionary<string, Stat> containerStats = DSGStats[container];
                    foreach (Stat aStat in containerStats.Values)
                    {
                        // Select out only the SyncConnector stats.
                        SyncConnectorStat connStat = aStat as SyncConnectorStat;
                        if (connStat != null)
                        {
                            lastStat = connStat;    // remember one of the stats for line output info
                            outputValues.Add(connStat.Name, connStat.Value);
                        }
                    }

                    // Get the log file writer for this connection and create one if necessary.
                    if (lastStat != null)
                    {
                        if (!ConnectionLoggers.TryGetValue(container, out connWriter))
                        {
                            string headr = LogSyncConnectorFilenamePrefix;
                            headr = headr.Replace("%CONTAINER%", container);
                            headr = headr.Replace("%REGIONNAME%", lastStat.RegionName);
                            headr = headr.Replace("%CONNECTIONNUMBER%", lastStat.ConnectorNum.ToString());
                            headr = headr.Replace("%THISACTOR%", lastStat.MyActorID);
                            headr = headr.Replace("%OTHERSIDEACTOR%", lastStat.OtherSideActorID);
                            headr = headr.Replace("%OTHERSIDEREGION%", lastStat.OtherSideRegionName);
                            headr = headr.Replace("%MESSAGETYPE%", lastStat.MessageType);
                            connWriter = new LogWriter(LogSyncConnectorDirectory, headr, LogSyncConnectorFileTimeMinutes, LogSyncConnectorFlushWrites);
                            ConnectionLoggers.Add(container, connWriter);

                            if (LogSyncConnectorIncludeTitleLine)
                            {
                                StringBuilder bufft = new StringBuilder();
                                bufft.Append("Region");
                                bufft.Append(",");
                                bufft.Append("SyncConnNum");
                                bufft.Append(",");
                                bufft.Append("ActorID");
                                bufft.Append(",");
                                bufft.Append("OtherSideActorID");
                                bufft.Append(",");
                                bufft.Append("OtherSideRegionName");
                                foreach (string fld in fields)
                                {
                                    bufft.Append(",");
                                    bufft.Append(fld);
                                }

                                connWriter.Write(bufft.ToString());
                            }
                        }

                        LastStatValues lastValues;
                        if (!m_lastStatValues.TryGetValue(container, out lastValues))
                        {
                            lastValues = new LastStatValues();
                            m_lastStatValues.Add(container, lastValues);

                        }
                        // Compute some useful values
                        ComputePerSecond("Msgs_Sent", "Msgs_Sent_Per_Sec", ref outputValues, ref lastValues.lastMsgs_Sent, msSinceLast);
                        ComputePerSecond("Msgs_Rcvd", "Msgs_Rcvd_Per_Sec", ref outputValues, ref lastValues.lastMsgs_Rcvd, msSinceLast);
                        ComputePerSecond("Bytes_Sent", "Bytes_Sent_Per_Sec", ref outputValues, ref lastValues.lastBytes_Sent, msSinceLast);
                        ComputePerSecond("Bytes_Rcvd", "Bytes_Rcvd_Per_Sec", ref outputValues, ref lastValues.lastBytes_Rcvd, msSinceLast);

                        StringBuilder buff = new StringBuilder();
                        buff.Append(lastStat.RegionName);
                        buff.Append(",");
                        buff.Append(lastStat.ConnectorNum.ToString());
                        buff.Append(",");
                        buff.Append(lastStat.MyActorID);
                        buff.Append(",");
                        buff.Append(lastStat.OtherSideActorID);
                        buff.Append(",");
                        buff.Append(lastStat.OtherSideRegionName);
                        foreach (string fld in fields)
                        {
                            buff.Append(",");
                            buff.Append(outputValues.ContainsKey(fld) ? outputValues[fld].ToString() : "");
                        }

                        // buff.Append(outputValues.ContainsKey("NewScript_Sent") ? outputValues["NewScript_Sent"] : "");
                        // buff.Append(outputValues.ContainsKey("NewScript_Rcvd") ? outputValues["NewScript_Rcvd"] : "");
                        // buff.Append(outputValues.ContainsKey("UpdateScript_Sent") ? outputValues["UpdateScript_Sent"] : "");
                        // buff.Append(outputValues.ContainsKey("UpdateScript_Rcvd") ? outputValues["UpdateScript_Rcvd"] : "");
                        // buff.Append(outputValues.ContainsKey("Terrain") ? outputValues["Terrain"] : "");
                        // buff.Append(outputValues.ContainsKey("GetObjects") ? outputValues["GetObjects"] : "");
                        // buff.Append(outputValues.ContainsKey("GetPresences") ? outputValues["GetPresences"] : "");
                        // buff.Append(outputValues.ContainsKey("GetTerrain") ? outputValues["GetTerrain"] : "");
                        connWriter.Write(buff.ToString());
                    }
                }
            }
            lastStatTime = Util.EnvironmentTickCount();
        }
        private void LogConnectorStats()
        {
            List <string> fields = new List <string>
            {
                "Msgs_Sent", "Msgs_Rcvd", "Bytes_Sent", "Bytes_Rcvd",
                "Msgs_Sent_Per_Sec", "Msgs_Rcvd_Per_Sec", "Bytes_Sent_Per_Sec", "Bytes_Rcvd_Per_Sec",
                "Queued_Msgs",
                "UpdatedProperties_Sent", "UpdatedProperties_Rcvd",
                "NewObject_Sent", "NewObject_Rcvd", "NewPresence_Sent", "NewPresence_Rcvd"
            };

            // Milliseconds since the last time we collected statistics
            int msSinceLast = Util.EnvironmentTickCountSubtract(lastStatTime);

            SortedDictionary <string, SortedDictionary <string, Stat> > DSGStats;

            if (StatsManager.TryGetStatsForCategory(DSGDetailCategory, out DSGStats))
            {
                foreach (string container in DSGStats.Keys)
                {
                    LogWriter                   connWriter   = null;
                    SyncConnectorStat           lastStat     = null;
                    Dictionary <string, double> outputValues = new Dictionary <string, double>();

                    SortedDictionary <string, Stat> containerStats = DSGStats[container];
                    foreach (Stat aStat in containerStats.Values)
                    {
                        // Select out only the SyncConnector stats.
                        SyncConnectorStat connStat = aStat as SyncConnectorStat;
                        if (connStat != null)
                        {
                            lastStat = connStat;    // remember one of the stats for line output info
                            outputValues.Add(connStat.Name, connStat.Value);
                        }
                    }

                    // Get the log file writer for this connection and create one if necessary.
                    if (lastStat != null)
                    {
                        if (!ConnectionLoggers.TryGetValue(container, out connWriter))
                        {
                            string headr = LogSyncConnectorFilenamePrefix;
                            headr      = headr.Replace("%CONTAINER%", container);
                            headr      = headr.Replace("%REGIONNAME%", lastStat.RegionName);
                            headr      = headr.Replace("%CONNECTIONNUMBER%", lastStat.ConnectorNum.ToString());
                            headr      = headr.Replace("%THISACTOR%", lastStat.MyActorID);
                            headr      = headr.Replace("%OTHERSIDEACTOR%", lastStat.OtherSideActorID);
                            headr      = headr.Replace("%OTHERSIDEREGION%", lastStat.OtherSideRegionName);
                            headr      = headr.Replace("%MESSAGETYPE%", lastStat.MessageType);
                            connWriter = new LogWriter(LogSyncConnectorDirectory, headr, LogSyncConnectorFileTimeMinutes, LogSyncConnectorFlushWrites);
                            ConnectionLoggers.Add(container, connWriter);

                            if (LogSyncConnectorIncludeTitleLine)
                            {
                                StringBuilder bufft = new StringBuilder();
                                bufft.Append("Region");
                                bufft.Append(",");
                                bufft.Append("SyncConnNum");
                                bufft.Append(",");
                                bufft.Append("ActorID");
                                bufft.Append(",");
                                bufft.Append("OtherSideActorID");
                                bufft.Append(",");
                                bufft.Append("OtherSideRegionName");
                                foreach (string fld in fields)
                                {
                                    bufft.Append(",");
                                    bufft.Append(fld);
                                }

                                connWriter.Write(bufft.ToString());
                            }
                        }

                        LastStatValues lastValues;
                        if (!m_lastStatValues.TryGetValue(container, out lastValues))
                        {
                            lastValues = new LastStatValues();
                            m_lastStatValues.Add(container, lastValues);
                        }
                        // Compute some useful values
                        ComputePerSecond("Msgs_Sent", "Msgs_Sent_Per_Sec", ref outputValues, ref lastValues.lastMsgs_Sent, msSinceLast);
                        ComputePerSecond("Msgs_Rcvd", "Msgs_Rcvd_Per_Sec", ref outputValues, ref lastValues.lastMsgs_Rcvd, msSinceLast);
                        ComputePerSecond("Bytes_Sent", "Bytes_Sent_Per_Sec", ref outputValues, ref lastValues.lastBytes_Sent, msSinceLast);
                        ComputePerSecond("Bytes_Rcvd", "Bytes_Rcvd_Per_Sec", ref outputValues, ref lastValues.lastBytes_Rcvd, msSinceLast);

                        StringBuilder buff = new StringBuilder();
                        buff.Append(lastStat.RegionName);
                        buff.Append(",");
                        buff.Append(lastStat.ConnectorNum.ToString());
                        buff.Append(",");
                        buff.Append(lastStat.MyActorID);
                        buff.Append(",");
                        buff.Append(lastStat.OtherSideActorID);
                        buff.Append(",");
                        buff.Append(lastStat.OtherSideRegionName);
                        foreach (string fld in fields)
                        {
                            buff.Append(",");
                            buff.Append(outputValues.ContainsKey(fld) ? outputValues[fld].ToString() : "");
                        }

                        // buff.Append(outputValues.ContainsKey("NewScript_Sent") ? outputValues["NewScript_Sent"] : "");
                        // buff.Append(outputValues.ContainsKey("NewScript_Rcvd") ? outputValues["NewScript_Rcvd"] : "");
                        // buff.Append(outputValues.ContainsKey("UpdateScript_Sent") ? outputValues["UpdateScript_Sent"] : "");
                        // buff.Append(outputValues.ContainsKey("UpdateScript_Rcvd") ? outputValues["UpdateScript_Rcvd"] : "");
                        // buff.Append(outputValues.ContainsKey("Terrain") ? outputValues["Terrain"] : "");
                        // buff.Append(outputValues.ContainsKey("GetObjects") ? outputValues["GetObjects"] : "");
                        // buff.Append(outputValues.ContainsKey("GetPresences") ? outputValues["GetPresences"] : "");
                        // buff.Append(outputValues.ContainsKey("GetTerrain") ? outputValues["GetTerrain"] : "");
                        connWriter.Write(buff.ToString());
                    }
                }
            }
            lastStatTime = Util.EnvironmentTickCount();
        }