/// <summary>   Post a string message to the server log.
 ///
 /// The implementation of this method constructs a SIF_LogEntry object
 /// with the <c>LogLevel</c> attribute set to "Info" and the
 /// <c>SIF_Desc</c> element set to the text message passed to
 /// the <c>message</c> parameter. The SIF_LogEntry object is then
 /// reported to the zone as a SIF_Event by delegating to the <c>log</c>
 /// method that accepts a SIF_LogEntry parameter.
 ///
 /// </summary>
 /// <param name="zone">The zone on the server to post the message to
 /// </param>
 /// <param name="message">The message text
 /// </param>
 public void Log(IZone zone,
                 String message)
 {
     //	If SIF 1.5 or later, encapsulate in a SIF_LogEntry and
     //	report it to the zone. Otherwise just write the message
     //	to the local zone log.
     if (Adk.SifVersion.CompareTo(SifVersion.SIF15r1) >= 0)
     {
         SIF_LogEntry le = new SIF_LogEntry();
         le.SetLogLevel(LogLevel.INFO);
         le.SIF_Desc = message;
         Log(zone, le);
     }
     else
     {
         zone.Log.Debug(message);
     }
 }
Ejemplo n.º 2
0
        /// <summary>   Post a SIF_LogEntry to the server.
        ///
        /// Use this form of the <c>log</c> method to post an error, warning,
        /// or informative message to the server that references a
        /// SIF Message and optionally a set of SIF Data Objects previously received
        /// by the agent. The log entry is assigned a category and code defined by
        /// the SIF Specification, and may have an extended error description and
        /// optional application-defined error code.
        ///
        /// </summary>
        /// <param name="level">The LogLevel to assign to this log entry
        /// </param>
        /// <param name="desc">A textual description of the error
        /// </param>
        /// <param name="extDesc">Extended error description, or <c>null</c> if no
        /// value is to be assigned to the SIF_LogEntry/SIF_ExtDesc element
        /// </param>
        /// <param name="appCode">Error code specific to the application posting the log
        /// entry, or <c>null</c> if no value is to be assigned to the
        /// SIF_LogEntry/SIF_ApplicationCode element
        /// </param>
        /// <param name="category">The SIF_Category value to assign to this log entry, as
        /// defined by the SIF Specification
        /// </param>
        /// <param name="category">The SIF_Code value to assign to this log entry, as
        /// defined by the SIF Specification
        /// </param>
        /// <param name="info">The <i>SifMessageInfo</i> instance from the Adk message
        /// handler implementation identifying a SIF Message received by the agent
        /// </param>
        /// <param name="objects">One or more SifDataObject instances received in the message
        /// identified by the <i>info</i> parameter
        /// </param>
        public virtual void Log(LogLevel level,
                                string desc,
                                string extDesc,
                                string appCode,
                                int category,
                                int code,
                                SifMessageInfo info,
                                params SifDataObject [] objects)
        {
            if (fZone == null)
            {
                throw new SystemException
                          ("ServerLog.log can only be called on a zone's ServerLog instance");
            }

            string       msg = null;
            SIF_LogEntry le  = null;

            if (Adk.SifVersion.CompareTo(SifVersion.SIF15r1) >= 0)
            {
                //	Create a SIF_LogEntry
                le = new SIF_LogEntry();
                le.SetSource(LogSource.AGENT);
                le.SetLogLevel(LogLevel.Wrap(level == null ? "Unknown" : level.ToString()));
                if (desc != null)
                {
                    le.SIF_Desc = desc;
                }
                if (extDesc != null)
                {
                    le.SIF_ExtendedDesc = extDesc;
                }
                if (appCode != null)
                {
                    le.SIF_ApplicationCode = appCode;
                }
                if (category != -1)
                {
                    le.SIF_Category = category.ToString();
                }
                if (code != -1)
                {
                    le.SIF_Code = code;
                }

                //	Reference a SIF_Message?
                if (info != null)
                {
                    try {
                        SIF_Header         headerCopy = (SIF_Header)info.SIFHeader.Clone();
                        SIF_LogEntryHeader sleh       = new SIF_LogEntryHeader();
                        sleh.SIF_Header = headerCopy;

                        //	Assign to SIF_OriginalHeader
                        le.SIF_OriginalHeader = sleh;
                    }
                    catch (Exception ex) {
                        fZone.Log.Warn
                            ("Unable to clone SIF_Header for SIF_LogEntry event:" + ex.Message, ex);
                    }
                }

                if (objects != null)
                {
                    SIF_LogObjects slos = new SIF_LogObjects();
                    le.SIF_LogObjects = slos;

                    for (int i = 0; i < objects.Length; i++)
                    {
                        if (objects[i] == null)
                        {
                            continue;
                        }

                        //	Package into a SIF_LogObject and add to the repeatable list
                        //	of SIF_LogEntry/SIF_LogObjects
                        SIF_LogObject lo = new SIF_LogObject();
                        lo.ObjectName = objects[i].ObjectType.Tag(info.SifVersion);
                        lo.AddChild((SifElement)objects[i].Clone());
                        slos.Add(lo);
                    }
                }
            }
            else
            {
                //  When running in SIF 1.1 or earlier, there is no
                //	SIF_LogEntry support. Build a string that can be
                //	written to the local zone log, including as much
                //	information from the would-be SIF_LogEntry as
                //	possible.

                StringBuilder b = new StringBuilder();

                b.Append("Server Log [Level=");
                b.Append(level == null ? "Unknown" : level.ToString());

                if (category != -1 && code != -1)
                {
                    b.Append(", Category=");
                    b.Append(category);
                    b.Append(", Code=");
                    b.Append(code);
                }
                if (appCode != null)
                {
                    b.Append(", AppCode=");
                    b.Append(appCode);
                }

                b.Append("] ");

                if (desc != null)
                {
                    b.Append(desc);
                }
                if (extDesc != null)
                {
                    b.Append(". " + extDesc);
                }

                msg = b.ToString();
            }

            //	Post the the server
            IServerLogModule [] chain = _getLogChain(fZone);
            for (int i = 0; i < chain.Length; i++)
            {
                if (le != null)
                {
                    chain[i].Log(fZone, le);
                }
                else
                {
                    chain[i].Log(fZone, msg);
                }
            }
        }