Conditionally Log a Message

Listing 4. This stored procedure conditionally logs a message if a provided address is a microsoft.com host. If the supplied address is an IP address, the stored procedure will attempt to perform a reverse DNS lookup to obtain a fully qualified hostname before checking whether the hostname ends in "microsoft.com."

[Microsoft.SqlServer.Server.SqlProcedure(Name="SP_OutputParamDemo")]
public static int ConditionalLog(SqlString msg, ref SqlString Address)
{
    // Do not proceed if there is no Address
    if (Address.IsNull)
    {
        return -1;
    }

    // Default return value
    int ret = 0;

    // If Address is an IP address, then try to resolve the hostname
    if (System.Text.RegularExpressions.Regex.Match(Address.Value, 
           @"^(?:\d{1,3}\.){3}\d{1,3}$").Success)
    {
        // Get host name for the provided IP address
        System.Net.IPHostEntry host =  
            System.Net.Dns.GetHostEntry(Address.Value);

        Address = host.HostName;
    }
       

    if (Address.Value.EndsWith(".microsoft.com", 
           StringComparison.CurrentCultureIgnoreCase))
    {
        // Set a different return value
        ret = 1;

        // Actually log the message here
    }

    return ret;
}