PowerSNMP for .NET
Log Event
Example 




Used to log SNMP messages sent or received.
Syntax
Remarks
Useful for capturing discovery packets.
Example
The following example demonstrates using the Log event to record traffic or update a UI.
//Call file.Close() when the app shuts down
FileStream file = new FileStream("log.txt", FileMode.Append, FileAccess.Write);

private void manager1_Log(object sender, LogEventArgs e)
{
    //Write SNMP packets to a file
    string label = (e.Message.Origin == null) ? "\r\nSent:\r\n" : "\r\nReceived:\r\n";
    file.Write(System.Text.Encoding.Default.GetBytes(label), 0, label.Length);
    file.Write(e.Message.EncodedMessage, 0, e.Message.EncodedMessage.Length);

    //Indicate in UI that a message was received
    if (e.Message is ResponseMessage)
        label1.Text += System.DateTime.Now.ToString() + ": " + e.Message.Origin.ToString() + ": " +
            "Response received with " + e.Message.Variables.Count.ToString() + " variable(s).\r\n";
}
'Call file.Close() when the app shuts down
Private file As New FileStream("log.txt", FileMode.Append, FileAccess.Write)

Private Sub manager1_Log(ByVal sender As Object, ByVal e As LogEventArgs)
    'Write SNMP packets to a file
    Dim label As String = If(e.Message.Origin Is Nothing, vbCrLf & "Sent:" & vbCrLf, vbCrLf & "Received:" & vbCrLf)
    file.Write(System.Text.Encoding.Default.GetBytes(label), 0, label.Length)
    file.Write(e.Message.EncodedMessage, 0, e.Message.EncodedMessage.Length)

    'Indicate in UI that a message was received
    If TypeOf e.Message Is ResponseMessage Then
        label1.Text &= Date.Now.ToString() & ": " & e.Message.Origin.ToString() & ": " & "Response received with " & e.Message.Variables.Count.ToString() & " variable(s)." & vbCrLf
    End If
End Sub
See Also

Reference

SnmpBase Class
SnmpBase Members


PowerSNMP for .NET Documentation Version 8.0
© 2024 Dart Communications. All Rights Reserved.
Send comments on this topic