Log

Log Class

The Log class handles script logging and error reporting

Syntax

public class Log : InstanceInterface

Remarks

A simple script for viewing the log messages might be:

C# Example
/* This content is licensed under the terms of the Creative Commons Attribution 4.0 International License.
 * When using this content, you must:
 * �    Acknowledge that the content is from the Sansar Knowledge Base.
 * �    Include our copyright notice: "� 2017 Linden Research, Inc."
 * �    Indicate that the content is licensed under the Creative Commons Attribution-Share Alike 4.0 International License.
 * �    Include the URL for, or link to, the license summary at https://creativecommons.org/licenses/by-sa/4.0/deed.hi (and, if possible, to the complete license terms at https://creativecommons.org/licenses/by-sa/4.0/legalcode.
 * For example:
 * "This work uses content from the Sansar Knowledge Base. � 2017 Linden Research, Inc. Licensed under the Creative Commons Attribution 4.0 International License (license summary available at https://creativecommons.org/licenses/by/4.0/ and complete license terms available at https://creativecommons.org/licenses/by/4.0/legalcode)."
 */
using Sansar.Script;
using Sansar.Simulation;
using System;
using System.Collections.Generic;
using System.Text;

// Put this script in 1 object in the scene. Use it via chat:
//   /console show
//         Show a dialog of the script logs
//   /console clear
//         Clear the script log
//   /console help
//         Show a list of console commands this script supports.
public class LogExample : SceneObjectScript
{
    // The trigger word can be set in the editor
    public string Trigger = "/console";

    // Init will be called by the script loader after the constructor and after any public fields have been initialized.
    public override void Init()
    {
        Log.Write("Console Example started.");
        chatHandlers["show"] = Show;
        chatHandlers["help"] = Help;
        chatHandlers["clear"] = Clear;

        // No command defaults to show
        chatHandlers[""] = Show;

        ScenePrivate.Chat.Subscribe(0, null, OnChat);
    }
    
    private Dictionary<string, Action<AgentPrivate>> chatHandlers = new Dictionary<string, Action<AgentPrivate>>();
    
    // send the log messages
    void Show(AgentPrivate agent)
    {
        StringBuilder list = new StringBuilder();
        foreach (var message in Log.Messages)
        {
            list.AppendLine(message.Text);
        }
        list.AppendLine("End of messages");
        agent.SendChat(list.ToString());
    }

    // send the help text
    void Help(AgentPrivate agent)
    {
        StringBuilder commands = new StringBuilder();
        commands.Append("Recognized commands are ");

        foreach (string command in chatHandlers.Keys)
        {
            commands.AppendFormat("{0} ", command);
        }
        agent.SendChat(commands.ToString());
    }

    // clear the log
    void Clear(AgentPrivate agent)
    {
        Log.Clear();
        agent.SendChat("Console log cleared");
    }
    
    void OnChat(int Channel, string Source, SessionId SourceId, ScriptId SourceScriptId, string Message)
    {
        // ignore any messages that are not from an agent
        if (SourceId != SessionId.Invalid)
        {
            AgentPrivate agent= ScenePrivate.FindAgent(SourceId);
            if (agent == null)
            {
                Log.Write(LogLevel.Warning, "Unable to find the agent who was talking.");
                return;
            }
            if (Message.StartsWith(Trigger))
            {
                string command = Message.Substring(Trigger.Length).Trim();

                if(chatHandlers.ContainsKey(command))
                {
                    chatHandlers[command](agent);
                }
                else
                {
                    chatHandlers["help"](agent);
                }
            }
        }
    }
}

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0

Members

See Also: Inherited members from InstanceInterface.

Public Fields

const
ConsoleHistory int (128). The maximum number of messages to store on this console.

Public Properties

[read-only]
 
Messages IEnumerable<Log.Message> . All current log messages.

Public Methods

  Clear ()
Clears all messages in this console.
  Clear (LogLevel)
Clears all messages in this console with the given logLevel.
  Clear (ScriptId)
Clears all messages in this console with the given ScriptId.
  Clear (string)
Clears all messages in this console with the given tag.
  Write (string)
Writes a debug message to the server log.
  Write (LogLevelstring)
Writes a debug message to the server log.
  Write (stringstring)
Writes a debug message to the server log.
  Write (LogLevelstringstring)
Writes a message to the script console.

Member Details

Clear Method

Clears all messages in this console.

Syntax

public void Clear ()

Remarks

Messages cannot be restored.

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0

Clear Method

Clears all messages in this console with the given logLevel.

Syntax

public void Clear (LogLevel logLevel)

Parameters

logLevel
The LogLevel to clear from the log. Multiple loglevels can be cleared in a single cal.

Remarks

Messages cannot be restored.

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0

Clear Method

Clears all messages in this console with the given ScriptId.

Syntax

public void Clear (ScriptId scriptId)

Parameters

scriptId
The ScriptId to clear from the log.

Remarks

Messages cannot be restored.

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0

Clear Method

Clears all messages in this console with the given tag.

Syntax

public void Clear (string tag)

Parameters

tag
The tag to clear from the log.

Remarks

Messages cannot be restored.

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0

ConsoleHistory Field

The maximum number of messages to store on this console.

Value: 128

Syntax

public const int ConsoleHistory

Returns

The maximum number of messages stored by the console.

Remarks

 

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0

Messages Property

All current log messages.

Syntax

public IEnumerable<Log.Message> Messages { get; }

Value

All currently stored log messages.

Remarks

 

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0

Write Method

Writes a debug message to the server log.

Syntax

public void Write (string message)

Parameters

message
The message to be logged.

Remarks

Log messages may be throttled.

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0

Write Method

Writes a debug message to the server log.

Syntax

public void Write (LogLevel logLevel, string message)

Parameters

logLevel
Message level
message
The message to be logged.

Remarks

Log messages may be throttled.

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0

Write Method

Writes a debug message to the server log.

Syntax

public void Write (string tag, string message)

Parameters

tag
A freeform string tag for filtering the message.
message
The message to be logged.

Remarks

Log messages may be throttled.

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0

Write Method

Writes a message to the script console.

Syntax

public void Write (LogLevel logLevel, string tag, string message)

Parameters

logLevel
LogLevel of the message.
tag
Tag for the message.
message
Message to log.

Remarks

Also tracks the sending script and the time. Only the last Log.ConsoleHistory messages are retained.

Requirements

Namespace: Sansar.Script
Assembly: Sansar.Script (in Sansar.Script.dll)
Assembly Versions: 1.0.0.0


Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Article is closed for comments.