PowerSNMP for .NET
Log Event (SnmpBase)

Used to log SNMP messages sent or received.
Useful for capturing discovery packets.
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


SnmpBase Class
SnmpBase Members

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